How do you specify an HTTP status code in Cakephp?

2020-02-24 12:49发布

In my controller, I check a condition to see if the user is allowed to do something. If the check fails, I want to send a 403 back to the browser. How do I do that in Cakephp?

标签: cakephp
9条回答
小情绪 Triste *
2楼-- · 2020-02-24 13:17

In CakePHP 2, the preferred method is to throw an exception:

throw new ForbiddenException();
查看更多
Explosion°爆炸
3楼-- · 2020-02-24 13:19

Perhaps something in this section of the cakephp manual can help you.

redirect(string $url, integer $status, boolean $exit)

The flow control method you’ll use most often is redirect(). This method takes its first parameter in the form of a CakePHP-relative URL. When a user has successfully placed an order, you might wish to redirect them to a receipt screen. The second parameter of redirect() allows you to define an HTTP status code to accompany the redirect. You may want to use 301 (moved permanently) or 303 (see other), depending on the nature of the redirect.

The method will issue an exit() after the redirect unless you set the third parameter to false.

查看更多
手持菜刀,她持情操
4楼-- · 2020-02-24 13:24

I'm adding in my two cents here because I don't feel like any of these answers covered this topic as thoroughly as I would have liked (at least for Cake 2.x).

If you want to throw an error status, use the Exception classes (as mentioned in other answers):

throw new BadRequestException(); // 400 Bad Request

// Or customize the code...
throw new BadRequestException('Custom error message', 405); // 405 Method Not Allowed

Fun fact: Cake will automatically do some magical error rendering even for RESTful calls via the ExceptionRenderer class. Even more fun of a fact is that it's based on the Status Code, not the fact that an Exception might have been thrown, so if you set the status code to > 400 on your own you will likely get error messages even if you didn't want them.

If you want to return a specific status code for a REST JSON/XML endpoint, take advantage of the new CakeResponse object, but also make sure that you add the special _serialize variable or you'll end up with a 'view not found' error as cake will attempt to find a view to render your JSON/XML. (This is by design - see the JsonView/XmlView class.)

$this->response->setStatus(201);  // 201 Created
$this->set('_serialize', array()); // Value must be something other than null

And lastly, if you want to send a non-200 status for a regularly rendered page, you can just use the setStatus() method with nothing else as mentioned in a previous answer:

$this->response->setStatus(201);

UPDATE:

$this->response->setStatus('code');

is no longer available. Use

$this->response->statusCode('code');
查看更多
贼婆χ
5楼-- · 2020-02-24 13:30

You can use cakephp response for custom message:

$this->response->header('HTTP/1.0 201', 'custom message');
$this->response->send();
查看更多
乱世女痞
6楼-- · 2020-02-24 13:35

It has changed again since CakePHP 3.6:

Use now

    $this->setResponse($this->response->withStatus(403) );
    return $this->response; // use this line also 

instead of

    $response = $this->response->withStatus(403);

https://api.cakephp.org/3.7/class-Cake.Controller.Controller.html#_setResponse

查看更多
兄弟一词,经得起流年.
7楼-- · 2020-02-24 13:36
$this->response->statusCode(403);

Will set the status code when Cake is ready to send the response. CakeResponse::send() expects to send the status code and message, so in my tests I think my using header() was getting overwritten. using $this->header('HTTP/1.1 400 Bad Request') doesn't work either because Cake expects any call to $this->header to be split on a colon ex: $this->header('Location: ...')

查看更多
登录 后发表回答