Silverstripe 中创建的响应之间的区别
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;
}
补充一下,就是return$this->response;或 return $this->getResponse();是必要的还是隐含的?
没有区别,打开父class定义,看看getResponse()做了什么:
public function getResponse() {
return $this->response;
}
当你想return HTTP错误时,最好使用
$this->httpError(400, 'invalid request');
(不需要 return,因为它会抛出异常)
经过测试,下面的代码产生了相同的结果。我的问题是,这两者有区别吗?
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;
}
补充一下,就是return$this->response;或 return $this->getResponse();是必要的还是隐含的?
没有区别,打开父class定义,看看getResponse()做了什么:
public function getResponse() {
return $this->response;
}
当你想return HTTP错误时,最好使用
$this->httpError(400, 'invalid request');
(不需要 return,因为它会抛出异常)