duanhe1965 2017-01-25 16:16
浏览 22
已采纳

无法在使用Cake PHP的HTTP响应中设置自定义状态代码消息

I want to modify the reponse in a controller method. I figured out how to change the status code, but I can't change the message. The API says to give to the httpCodes method an array in which for each code, the message we want to set. Here is my code :

$this->response->statusCode(400);   
$this->response->httpCodes(array(400 => 'Origin Denied'));
return $this->response;

And I get Bad Request instead of Origin Denied.

I also tried to directly set the header like this :

$this->response->header("HTTP/1.0 500 Invalid file name.");

or

$this->response->header("HTTP/1.0", "500 Invalid file name.");

But I get

 "HTTP/1.0 500 Invalid file name." is not valid header name : InvalidArgumentException 

I'm on Cake PHP 3.3 and PHP 7.1. The goal of this is to upload medias on my website and to get back a JSON structure with the file location, or an appropriate code if it fails. This is TinyMCE requirements.

I'm a beginner with CakePHP and the Response class, I read the book and the API but I still don't know how to do it.

  • 写回答

2条回答 默认 最新

  • duan198299 2017-01-25 17:31
    关注

    First of all I'd like to note that such a requirement seems rather inconvenient. If the reponse is to be expected to have a body in JSON format, then the custom reason phrase could be easily defined just in there.

    Response::httpCodes() only works with the "old" front controller

    That being said, generally this is possible, but when using the PSR compatible request/reponse components introduced with CakePHP 3.3, this is currently not directly supported, as custom reason phrases will not be passed over to the PSR compatible response. With the pre 3.3 dispatching mechanisms it will still work however, check the "old" application templates front controller (webroot/index.php file).

    Can be done easily again as of 3.4

    As of CakePHP 3.4, Response::httpCodes() is deprecated, and it will be removed in 4.0. Also as of 3.4, the CakePHP response class will be fully PSR-7 compatible, and you will be able to set statuses with custom reason phrases via the Response::withStatus() method, like

    return $this->response->withStatus(400, 'Bad Origin');
    

    Have in mind that the PSR-7 compatible reponse objects are immutable! ie, if you wish to modify $this->response in your controller for further use, you'd have to overwrite it, like:

    $this->response = $this->response->withStatus(/* ... */);
    

    Requires "workaround" in 3.3/3.4 transition

    In the transition between 3.3 and 3.4, when using the PSR compatible front controller, you can add support for custom reason phrases by overriding Application::__invoke() (the Application class file is by default located in your apps src folder.

    You'd have to reimplement the BaseApplication::__invoke() code, and pass over the reason phrase obtained from Response::httpCodes(), something along the lines of:

    use Cake\Http\RequestTransformer;
    use Cake\Http\ResponseTransformer;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    
    // ...
    
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
    {
        $cakeRequest = RequestTransformer::toCake($request);
        $cakeResponse = ResponseTransformer::toCake($response);
    
        $cakeResponse = $this->getDispatcher()->dispatch($cakeRequest, $cakeResponse);
    
        $psrResponse = ResponseTransformer::toPsr($cakeResponse);
    
        $status = $psrResponse->getStatusCode();
        $httpCodes = $cakeResponse->httpCodes($status);
        if ($httpCodes !== null && isset($httpCodes[$status])) {
            return $psrResponse->withStatus($status, $httpCodes[$status]);
        }
    
        return $psrResponse;
    }
    

    See also

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题