php Cakephp:我如何将所有丢失的控制器/动作调用路由到一个单一的、通用的错误页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1745968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cakephp: How would I route all missing controller/action calls to a single, general error page?
提问by davethegr8
I've got a cakephp app that I'm trying to get to serve up the Pages::404function (and corresponding view) whenever Cake encounters any error (missing controller, action, etc).
我有一个 cakephp 应用程序,Pages::404每当 Cake 遇到任何错误(缺少控制器、动作等)时,我都会尝试提供该应用程序(和相应的视图)。
What's the best way to do that?
这样做的最佳方法是什么?
回答by deceze
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:
Cake 会因缺少方法或控制器而自动抛出 404 错误。在调试模式下,此错误采用包含说明的详细错误消息的形式,例如:
Missing Controller
Error: FooController could not be found.
Error: Create the class FooController below in file: > app/controllers/foo_controller.php
Notice: If you want to customize this error message, create app/views/errors/missing_controller.ctp
缺少控制器
错误:找不到 FooController。
错误:在文件中创建下面的类 FooController:> app/controllers/foo_controller.php
注意:如果要自定义此错误信息,请创建 app/views/errors/missing_controller.ctp
In production mode (debug = 0) the message just looks like this:
在生产模式 ( debug = 0) 中,消息如下所示:
Not Found
Error: The requested address '/foo' was not found on this server.
未找到
错误:在此服务器上找不到请求的地址“/foo”。
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/.
这些错误页面在cake/libs/view/errors/. 正如调试模式中的消息所说,您可以cake/在app/views/errors/.
If you want to execute a custom function on errors, you'll best put it in the AppErrorController as described in Error Handling.
如果要对错误执行自定义函数,最好将其放入AppError控制器中,如错误处理中所述。
回答by Sandip Layek
Step 1:In app_controller.php add two functions
第一步:在app_controller.php中添加两个函数
function _setErrorLayout() {
if ($this->name == 'CakeError') {
$this->layout = 'error';
}
}
function beforeRender () {
$this->_setErrorLayout();
}
}
Step2: In views\layouts\create error.ctpcontaining echo $content_for_layout;
Step2:在views\layouts\创建error.ctp包含 echo $content_for_layout;
step:3 In views\errors\make missing_action.ctpand customize the page as you need
my PHP code was:
步骤:3 在根据需要views\errors\制作missing_action.ctp和自定义页面时,我的 PHP 代码是:
echo $html->image('404-not-found-1-3.jpg');
回答by Michael Mao
Are you in the controller when you are trying to redirect to the 404 error page?
当您尝试重定向到 404 错误页面时,您是否在控制器中?
Well if that is the case, you can walk around the problem like this:
那么如果是这样的话,你可以像这样解决这个问题:
Copy and paste the error layout (error404.ctp) from the cake core library directory into yours app/views/errors/
将错误布局(error404.ctp)从 cake 核心库目录复制并粘贴到你的 app/views/errors/
Then add the following line whenever you encounter an error inside a controller.
然后每当您在控制器中遇到错误时添加以下行。
$this->cakeError('error404',array(array('url'=>'/')));
Oh, another way to handle this is to edit the routes.php file in app/config
哦,另一种处理方法是编辑 app/config 中的 routes.php 文件
CakePHP Official site Routes-Configuration
I don't have a working copy of CakePHP at the moment, so I would just describe the basic logic here(what you can do inside the routes.php file)
我目前没有 CakePHP 的工作副本,所以我只在这里描述基本逻辑(你可以在 routes.php 文件中做什么)
Redirect traffic with specific url patterns(say, http://yourwebsite/validController/validFunction/validParam) to their corresponding destinations respectively. Redirect all other traffic (missing controller, model, view, etc) to 404 page.
将具有特定 url 模式(例如http://yourwebsite/validController/validFunction/validParam)的流量分别重定向到相应的目的地。将所有其他流量(缺少控制器、模型、视图等)重定向到 404 页面。
Hope that helps:)
希望有帮助:)
回答by Ishan
Please add a class PostController.php not post_controller.php in the app/controller folder. It just mean that You have to create as like its class name.
请在 app/controller 文件夹中添加一个类 PostController.php 而不是 post_controller.php。这只是意味着你必须像它的类名一样创建。

