CakePHP 2.0 - 如何使自定义错误页面? [英] CakePHP 2.0 - How to make custom error pages?

查看:337
本文介绍了CakePHP 2.0 - 如何使自定义错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到AppError类现在是向后兼容性,应该使用异常。

I read that the AppError class is now for backwards compatibility and that Exceptions should be used instead. How does one go about creating custom error pages for things like 404 errors, or completely custom errors?

推荐答案

请尝试以下操作:

/app/Config/core.php

/app/Config/core.php

异常渲染需要设置为 AppExceptionRender 。示例:

Exception render need to set as an AppExceptionRender. Example:

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

/app/Controller/ErrorsController.php

/app/Controller/ErrorsController.php

class ErrorsController extends AppController {
    public $name = 'Errors';

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

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

/app/Lib/Error/AppExceptionRenderer.php

/app/Lib/Error/AppExceptionRenderer.php

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

class AppExceptionRenderer extends ExceptionRenderer {

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

/app/View/Errors/error404.ctp

/app/View/Errors/error404.ctp

<div class="inner404">
    <h2>404 Error - Page Not Found</h2>
</div>

在需要的地方插入: throw new NotFoundException();

这篇关于CakePHP 2.0 - 如何使自定义错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆