【问题标题】:Difference between created response in SilverstripeSilverstripe 中创建的响应之间的区别
【发布时间】:2016-03-25 03:43:10
【问题描述】:

经过测试,下面的代码产生了相同的结果。我的问题是,这两者有区别吗?

 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();是必要的还是隐含的?

【问题讨论】:

标签: php content-management-system silverstripe


【解决方案1】:

没什么区别,打开父类定义看看getResponse()是干什么的:

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

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

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

(不需要返回,因为它会抛出异常)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-11-30
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多