如何在Cakephp中指定HTTP状态代码? [英] How do you specify an HTTP status code in Cakephp?

查看:174
本文介绍了如何在Cakephp中指定HTTP状态代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我检查一个条件,看看用户是否被允许做某事。如果检查失败,我想发送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 - 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 -

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆