在我的控制器中,我检查一个条件,看看是否允许用户做某事.如果检查失败,我想将403发送回浏览器.我怎么在Cakephp中做到这一点?
dei*_*zel 39
编辑 - 这个问题很老,涵盖了CakePHP框架的不同版本.以下是每个答案适用的版本的摘要.不要忘记对最有帮助的解决方案进行投票.
编辑#2 - Mark37添加了CakePHP 2.x的更详细解答.
编辑#3 - 为CakePHP添加了解决方案.(2018年5月:CakePHP 3.5进行了一些功能重命名,Roberto的解决方案仍然有效.)
通过查看前一个注释中的相关API代码,您似乎可以调用Controller :: header($ status)来输出没有重定向的头.在您的情况下,最有可能正确使用:
$this->header('HTTP/1.1 403 Forbidden');
Run Code Online (Sandbox Code Playgroud)
Asa*_*ers 14
$this->response->statusCode(403);
Run Code Online (Sandbox Code Playgroud)
当Cake准备好发送响应时,将设置状态代码.CakeResponse :: send()期望发送状态代码和消息,因此在我的测试中我认为我的使用header()被覆盖了.使用$this->header('HTTP/1.1 400 Bad Request')不起作用,因为Cake期望任何调用$this->header在冒号ex上拆分:$this->header('Location: ...')
Mar*_* G. 10
我在这里加上我的两分钱,因为我不觉得这些答案中的任何一个都像我希望的那样彻底地涵盖了这个主题(至少对于Cake 2.x而言).
如果要抛出错误状态,请使用Exception类(如其他答案中所述):
throw new BadRequestException(); // 400 Bad Request
// Or customize the code...
throw new BadRequestException('Custom error message', 405); // 405 Method Not Allowed
Run Code Online (Sandbox Code Playgroud)
有趣的事实:即使对于通过ExceptionRenderer类的RESTful调用,Cake也会自动执行一些神奇的错误渲染.更有趣的事实是它基于状态代码,而不是Exception可能已被抛出的事实,所以如果您自己将状态代码设置为> 400,即使您没有,也可能会收到错误消息要他们.
如果要返回REST JSON/XML端点的特定状态代码,请利用新CakeResponse对象,但也要确保添加特殊_serialize变量,否则最终会出现"未查找视图"错误将尝试查找呈现JSON/XML的视图.(这是设计 - 见JsonView/ XmlViewclass.)
$this->response->setStatus(201); // 201 Created
$this->set('_serialize', array()); // Value must be something other than null
Run Code Online (Sandbox Code Playgroud)
最后,如果您想为常规呈现的页面发送非200状态,您可以使用该setStatus()方法,而不是之前的答案中提到的其他方法:
$this->response->setStatus(201);
Run Code Online (Sandbox Code Playgroud)
更新:
$this->response->setStatus('code');
Run Code Online (Sandbox Code Playgroud)
不再被提供.使用
$this->response->statusCode('code');
Run Code Online (Sandbox Code Playgroud)
在重新审视这个问题,并阅读Adriano对我之前回答的评论(关于将用户重定向到友好页面)时,我想出了一个新的解决方案.
在控制器中,您可以调用$this->cakeError('error404')以生成友好的404页面.这可以通过在' app/views/errors/error404.ctp' 创建文件来定制(与其他错误一样).
在仔细查看代码后cakeError,我的建议是尝试ErrorHandler通过在' app/error.php'或(可能更优选)' app/app_error.php' 创建文件来扩展Cake .
您error403的error404代码(模仿代码)可能如下所示:
class AppError extends ErrorHandler {
function error403($params) {
extract($params, EXTR_OVERWRITE);
$this->error(array(
'code' => '403',
'name' => 'Forbidden',
'message' => sprintf(__("Access was forbidden to the requested address %s on this server.", true), $url, $message)));
$this->_stop();
}
}
Run Code Online (Sandbox Code Playgroud)
你应该也能够通过创造"提供了这个错误自定义视图app/views/errors/error403.ctp".以下是error404视图的修改版本:
<h2><?php echo $name; ?></h2>
<p class="error">
<strong>Error: </strong>
<?php echo sprintf(__("Access was forbidden to the requested address %s on this server.", true), "<strong>'{$message}'</strong>")?>
</p>
Run Code Online (Sandbox Code Playgroud)
小智 7
自 CakePHP 3.6 起,它又发生了变化:
现在使用
$this->setResponse($this->response->withStatus(403) );
return $this->response; // use this line also
Run Code Online (Sandbox Code Playgroud)
代替
$response = $this->response->withStatus(403);
Run Code Online (Sandbox Code Playgroud)
https://api.cakephp.org/3.7/class-Cake.Controller.Controller.html#_setResponse
关于CakePHP 3.x的注释似乎丢失了,因此要使此线程完整:
对于CakePHP 3.x,请使用:
$response = $this->response->withStatus(403);
return $response;
Run Code Online (Sandbox Code Playgroud)
对于CakePHP 3.3.x之前的版本,可以使用与CakePHP 2.x相同的样式:
$this->response->statusCode('code');
Run Code Online (Sandbox Code Playgroud)
请注意,直接使用PHP函数也可以使用(http_response_code(403); die();),尽管使用响应对象似乎是预期的方法。