Cakephp:我如何将所有丢失的控制器/动作调用路由到一个单一的、通用的错误页面?

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

我有一个 cakephp 应用程序,每当 Cake 遇到任何错误(缺少控制器、动作等)时,我都会尝试使用它来提供 Pages::404 函数(和相应的视图)).

I've got a cakephp app that I'm trying to get to serve up the Pages::404 function (and corresponding view) whenever Cake encounters any error (missing controller, action, etc).

最好的方法是什么?

推荐答案

Cake 会因缺少方法或控制器而自动抛出 404 错误.在调试模式下,此错误采用包含说明的详细错误消息的形式,例如:

Cake automatically throws a 404 error for missing methods or controllers. While in debug mode, this error takes the form of a detailed error message containing instructions, like:

缺少控制器

错误:找不到 FooController.

Error: FooController could not be found.

错误:在文件中创建下面的类 FooController:>应用程序/控制器/foo_controller.php

Error: Create the class FooController below in file: > app/controllers/foo_controller.php

注意:如果要自定义此错误信息,请创建 app/views/errors/missing_controller.ctp

Notice: If you want to customize this error message, create app/views/errors/missing_controller.ctp

在生产模式 (debug = 0) 中,消息如下所示:

In production mode (debug = 0) the message just looks like this:

未找到

错误:在此服务器上找不到请求的地址/foo".

Error: The requested address '/foo' was not found on this server.

这些错误页面在 cake/libs/view/errors/ 中定义.正如调试模式中的消息所说,您可以在 app/views/errors/ 中创建自己的自定义错误页面(使用与 cake/ 目录中的名称相同的名称)代码>.

These error pages are defined in cake/libs/view/errors/. As the message in debug mode says, you can create your own, custom error pages (using the same name as the ones in the cake/ directory) in app/views/errors/.

如果您想对错误执行自定义函数,最好将其放入 AppError 控制器中,如 错误处理.

If you want to execute a custom function on errors, you'll best put it in the AppError Controller as described in Error Handling.

相关文章