2013-10-16 22 views
0

這是到目前爲止我有:CakePHP的2.x的自定義異常渲染

應用程序/配置/ core.php中

Configure::write('debug', 2); 

應用程序/配置/ bootstrap.php中

CakePlugin::loadAll(array('bootstrap' => true)); 

app/Plugin/Core/bootstrap.php

Configure::write('Exception.renderer', 'Core.AppExceptionRenderer'); 

應用/插件/核心/ lib中/錯誤/ AppExceptionRenderer.php

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

class AppExceptionRenderer extends ExceptionRenderer { 

    public function notFound($error) { 
     echo $error->getMessage(); 
    } 

    public function missingController($error) { 
     echo $error->getMessage(); 
    } 
} 

那些簡單的回聲工作。

現在我想要每個錯誤函數呈現(不重定向!)從Core插件的視圖,如app/Plugin/Core/View/Pages/error

我不想呈現靜態頁面(例如/Errors/error400.ctp),因爲錯誤頁面的內容可以由用戶從管理面板編輯。

應在主題Default中設置錯誤頁面佈局。

http://book.cakephp.org/2.0/en/development/exceptions.html

回答

0

您可以設置視圖這樣

應用/插件/核心/ lib中/錯誤/ AppExceptionRenderer.php

<?php 

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

class AppExceptionRenderer extends ExceptionRenderer { 

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

    public function missingController($error) { 
     $this->controller->redirect(array('controller' => 'custom_errors', 'action' => 'missing_controller')); 
    } 
} 

應用程序/控制器的自定義錯誤/CustomErrorsController.php

<?php 
App::uses('Controller', 'Controller'); 

class CustomErrorsController extends Controller { 

    public function beforeFilter(){ 

    } 

    public function not_found(){ 
     // your coding goes here. 

    } 

    public function missing_controller(){ 
     // your coding goes here. 

    } 
} 

您可以指定操作的視圖。

+2

哥們我說沒有重定向如果可能的話.. –

1

我想這是你在忙什麼,設置佈局&視圖在beforeFilter渲染方法 -

class AppExceptionRenderer extends ExceptionRenderer { 
    public function beforeFilter() { 
     $this->layout = 'YOUR_LAYOUT'; // Setting the default layout to your layout 
     $this->view = '../../Plugin/Core/View/Pages/error'; //Check this path to your ctp file 
    } 
... 
... 
}