問題描述
我有一個 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.
這篇關于Cakephp:我如何將所有丟失的控制器/動作調用路由到一個單一的、通用的錯誤頁面?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!