加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

CakePHP重定向,状态码为404

发布时间:2020-12-13 13:41:38 所属栏目:PHP教程 来源:网络整理
导读:对于Cake PHP错误,我知道存在CakeError和AppError解决方案. 但我需要在控制器内进行重定向. 在AppController中存在: function afterFilter() { if ($this-response-statusCode() == '404') { $this-redirect(array( 'controller' = 'mycontroller','action'
对于Cake PHP错误,我知道存在CakeError和AppError解决方案.
但我需要在控制器内进行重定向.

在AppController中存在:

function afterFilter() {
    if ($this->response->statusCode() == '404')
    {
        $this->redirect(array(
            'controller' => 'mycontroller','action' => 'error',404),404
        );
    }
}

但这不会创建404状态代码.它创建了一个302代码.
我把代码更改为:

$this->redirect('/mycontroller/error/404',404);

但结果是一样的.

我添加了这个,它不起作用也弃用:

$this->header('http/1.0 404 not found');

如何在控制器重定向中发送404代码?

如果要返回404错误,请使用内置的CakeError支持:
throw new NotFoundException();

您可以从控制器中抛出此异常,它应该生成404响应.

还有更多内置异常here.

如果您对制作自定义错误页面感兴趣,请参阅this post.

否则,我认为不可能返回404标头代码并仍然重定向. Http将重定向状态代码指定为be in the 300s,这是CakePHP遵循的约定.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读