抓住所有错误cakephp 2

时间:2017-02-15 04:56:27

标签: cakephp

我使用cakephp,当有错误时,cakephp为用户显示错误示例:undefine index,在视图页面中注意...我想要什么时候有错误cakephp只显示一个消息由我自定义在视图中。有人知道吗?非常感谢。

1 个答案:

答案 0 :(得分:0)

您可以制作自定义错误页面

/app/Config/core.php

异常呈现需要设置为AppExceptionRender。例如:

  

配置::写('异常',数组(           'handler'=> '的ErrorHandler :: handleException',           'renderer'=> 'AppExceptionRenderer',           'log'=>真));

/app/Controller/ErrorsController.php

  

类ErrorsController扩展AppController {       public $ name ='错误';

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('error404');
}

public function error404() {
    //$this->layout = 'default';
} }

/app/Lib/Error/AppExceptionRenderer.php

App :: uses('ExceptionRenderer','Error');

  

class AppExceptionRenderer扩展了ExceptionRenderer {

public function notFound($error) {
    $this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));
} }

/app/View/Errors/error404.ctp

  `在这里输入代码       

404错误 - 未找到页面

`在此输入代码`

将其插入您需要的地方:抛出新的NotFoundException();

之后确保调试模式为0, 它仅适用于生产模式 配置:: write('debug',0);