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 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档