問題描述
我讀到 AppError 類現在是為了向后兼容,應該使用 Exceptions 來代替.如何為 404 錯誤或完全自定義錯誤等內容創建自定義錯誤頁面?
試試這個:
/app/Config/core.php
異常渲染需要設置為AppExceptionRender
.示例:
Configure::write('Exception', array('處理程序' =>'ErrorHandler::handleException','渲染器' =>'AppExceptionRenderer','日志' =>真的));
/app/Controller/ErrorsController.php
class ErrorsController 擴展 AppController {公共 $name = '錯誤';公共函數 beforeFilter() {父::beforeFilter();$this->Auth->allow('error404');}公共函數 error404() {//$this->layout = 'default';}}
/app/Lib/Error/AppExceptionRenderer.php
App::uses('ExceptionRenderer', 'Error');類 AppExceptionRenderer 擴展 ExceptionRenderer {公共函數 notFound($error) {$this->controller->redirect(array('controller' => 'errors', 'action' => 'error404'));}}
/app/View/Errors/error404.ctp
<h2>404 錯誤 - 頁面未找到</h2>
在需要的地方插入:throw new NotFoundException();
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?
Try this:
/app/Config/core.php
Exception render need to set as an AppExceptionRender
. Example:
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>
Insert it where you need: throw new NotFoundException();
這篇關于CakePHP 2.0 - 如何制作自定義錯誤頁面?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!