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

2021-12-21 00:00:00 redirect php cakephp

控制器中是否有一种方法或功能可以判断是否触发了 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('/');
    }
}

手册

相关文章