我读到AppError类现在是为了向后兼容,应该改用Exceptions。如何为404错误或完全自定义错误之类的内容创建自定义错误页面?
尝试这个:
/app/Config/core.php
异常渲染需要设置为AppExceptionRender。例:
AppExceptionRender
Configure::write('Exception', array( 'handler' => 'ErrorHandler::handleException', 'renderer' => 'AppExceptionRenderer', 'log' => true ));
/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::uses('ExceptionRenderer', 'Error'); class AppExceptionRenderer extends ExceptionRenderer { public function notFound($error) { $this->controller->redirect(array('controller' => 'errors', 'action' => 'error404')); } }
/app/View/Errors/error404.ctp
<div class="inner404"> <h2>404 Error - Page Not Found</h2> </div>
将其插入您需要的位置: throw new NotFoundException();
throw new NotFoundException();