1

我试图将所有错误强制到 Cakephp(2.0) 中的单个自定义页面,但我发现的大多数教程都是针对非常具体的错误代码的。我希望它只显示一个错误页面,所以我首先要做的是将特定错误状态的所有错误指向一个错误页面。但经过进一步测试,我注意到其他代码没有被捕获。

我需要我的代码将所有 http 错误代码捕获到单个错误页面。

这是我的代码:

Configure::write('Exception', array(
    'handler' => 'ErrorHandler::handleException',
    'renderer' => 'AppExceptionRenderer',
    'log' => true
));

还有我的 AppExceptionRenderer:

<?php 
 App::uses('ExceptionRenderer', 'Error');

class AppExceptionRenderer extends ExceptionRenderer {

public function internalError($error) {
$this->controller->beforeFilter();
$this->controller->set('title_for_layout', 'Internal Server Error');
$this->controller->render('/Errors/error500');
$this->controller->response->send();

... All the other error handlers are similar to this, but very limited since there are a lot of client and server errors 
} ?>

我的模板页面似乎工作得很好,所以我没有包含它。

如何更改代码以覆盖每个 http 状态代码?还是不可能?

4

1 回答 1

1

您必须覆盖以下方法:

ExceptionRenderer::error400($error) 
ExceptionRenderer::error500($error) 

或者,您可以使用以下内容覆盖ExceptionRenderer:_outputMessage($template)

protected function _outputMessage($template) {
    $this->controller->layout = 'my_error';
    parent::_outputMessage($template);
}

并将文件 /app/View/Layouts/my_error.ctp 添加为新的自定义布局。

于 2015-11-27T18:11:54.693 回答