如何在Cakephp中指定HTTP状态代码? [英] How do you specify an HTTP status code in Cakephp?
问题描述
在我的控制器中,我检查一个条件,看看用户是否被允许做某事。如果检查失败,我想发送403回到浏览器。
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框架。以下是每个答案适用的版本的摘要。
EDIT - This question is quite old and covers different versions of the CakePHP framework. Following is a summary of which version each answer applies to. Don't forget to vote on the solution that helps most.
- CakePHP 2.x - 使用异常( Brad Koch的回答)[首选解决方案]
- CakePHP 2.x - 仅设置标题( Asa Ayers答案)
- CakePHP 1.x - 使用错误处理常式(我的其他答案)
- CakePHP 1.x - 仅设置标题(此回答)
- CakePHP 2.x - using exceptions (Brad Koch's answer) [preferred solution]
- CakePHP 2.x - setting header only (Asa Ayers' answer)
- CakePHP 1.x - using error handler (my other answer)
- CakePHP 1.x - setting header only (this answer)
EDIT #2 - A more detailed answer for CakePHP 2.x has been added by Mark37.
通过查看相关的< a href =http://api.cakephp.org/view_source/controller/#l-525> API代码,似乎可以调用 Controller :: header($ status)输出一个没有重定向的头。在您的情况下,正确的用法很可能是:
By looking at the relevant API code from the previous comment, it seems you can call Controller::header($status) to output a header without redirection. In your case, the proper usage is most likely:
$this->header('HTTP/1.1 403 Forbidden');
这篇关于如何在Cakephp中指定HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!