用户登录
用户注册

分享至

CakePHP:将所有 404 错误重定向到主页?

  • 作者: _微凉先森
  • 来源: 51数据库
  • 2023-02-02

问题描述

控制器中是否有一种方法或功能可以判断是否触发了 404 错误?我希望每个人都在首页重定向,而不是看到 404 页面.

Is there a way or a function within a controller that tells if a 404 error was triggered? I would like everyone to redirect at the homepage instead of seeing a 404 page.

谢谢!

推荐答案

要捕获和处理 404 错误,需要扩展 ErrorHandler 类并覆盖 error404 方法.为此,请使用以下代码创建文件 app/app_error.php:

To catch and handle 404 errors, you need to extend the ErrorHandler class and override the error404 method. To do that, create the file app/app_error.php with the following code:

class AppError extends ErrorHandler {
    function error404($params) {
        // redirect to homepage
        $this->controller->redirect('/');
    }
}

手册

软件
前端设计
程序设计
Java相关