Silversstripe中创建的响应之间的差异


Difference between created response in Silverstripe

测试后,下面的代码会产生相同的结果。我的问题是,两者之间有什么区别吗?

 public function someaction1(SS_HTTPRequest $request) {
        $this->setResponse(new SS_HTTPResponse());
        $this->getResponse()->setStatusCode(400);
        $this->getResponse()->setBody('invalid');
        return $this->getResponse();
    }
    public function someaction2(SS_HTTPRequest $request) {
        $this->response = new SS_HTTPResponse();
        $this->response->setStatusCode(400);
        $this->response->setBody('invalid');
        return $this->response;
    }

要添加,请返回$this->response;或返回$this->getResponse();必要的还是隐含的?

没有区别,只需打开父类定义,看看getResponse()的作用:

public function getResponse() {
    return $this->response;
}

当你想返回HTTP错误时,最好使用

$this->httpError(400, 'invalid request');

(无需返回,因为它抛出异常)