2010-10-10 60 views

回答

26

從IRC莎凡特幫了我和他在app_controller使用beforeRender(){}建議

// Before Render 
function beforeRender() { 
    if($this->name == 'CakeError') { 
     //$this->layout = 'error'; 
    } 
} 

CakeError是錯誤的統稱:d

+0

這種方法不再使用CakePHP 2.x的有效我不知道現在該怎麼做......大聲笑 – 2012-06-29 19:15:01

+3

我可以證實這種方法適用於Cake 2.2.1。 – bancer 2012-09-13 09:14:33

+0

我沒有在Cake 2.2.1中確認。我調試了beforeRender中的$ this-> layout,我得到了預期的'public_layout',但它仍然以default.ctp呈現。 DEBUG被設置爲0. – 2012-10-23 11:20:04

4

我知道這個簡單的方法是在你的AppController創建這個函數:但是

function appError($method, $messages) 
{ 
} 

那麼你可以做任何你想要的錯誤,顯示它你喜歡,或不顯示它在所有,發送電子郵件等。(我不知道這方法,如果仍然有效。)

也有在您的應用程序根創建app_error.php的選項,在它class AppError extends ErrorHandler,它使您能夠覆蓋各種錯誤。但我還沒有這樣做,所以我不能告訴你更多關於它的信息。

請參閱cake/libs/error.phpcake/libs/object.php,當然還有The Book瞭解更多信息。忘記提及,一旦你發現錯誤,沒有什麼能夠阻止你 - 例如 - 將錯誤存儲在會話中,重定向到你的「錯誤處理控制器」,然後顯示在你的控制器中你要。

+0

感謝很多文件,並設置佈局:d – 2010-10-10 13:04:00

10

更好地在您的應用程序創建一個error.php文件文件夾

class AppError extends ErrorHandler { 
    function error404($params) { 
      $this->controller->layout = 'error'; 
      parent::error404($params); 
    } 
} 

這樣就可以避免在每個頁面如果測試渲染學者解決方案引入了

14

在CakePHP 2.2.2,我改變了ExceptionRenderer在core.php中與我自己的,就像這樣:

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

Configure::write('Exception', array(
    'handler' => 'ErrorHandler::handleException', 
    'renderer' => 'MyExceptionRenderer', // this is ExceptionRenderer by default 
    'log' => true 
)); 

應用程序/庫/錯誤/ MyExceptionRenderer。 PHP:

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

class MyExceptionRenderer extends ExceptionRenderer { 

    protected function _outputMessage($template) { 
    $this->controller->layout = 'error'; 
    parent::_outputMessage($template); 
    } 

} 
+0

無法滿足這個要求,它勝過了我複製每個錯誤視圖並將佈局設置爲頂部「錯誤」的舊方法。 – Matthew 2014-03-08 13:31:29

4

我的解決方案爲CakePHP的2.3

變化率T他core.php中的ExceptionRenderer使用您自己的渲染器。

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

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

應用程序/庫/錯誤/ MyExceptionRenderer.php:

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

class MyExceptionRenderer extends ExceptionRenderer 
{ 
    /** 
    * Overrided, to always use a bare controller. 
    * 
    * @param Exception $exception The exception to get a controller for. 
    * @return Controller 
    */ 
    protected function _getController($exception) { 
     if (!$request = Router::getRequest(true)) { 
      $request = new CakeRequest(); 
     } 
     $response = new CakeResponse(array('charset' => Configure::read('App.encoding'))); 
     $controller = new Controller($request, $response); 
     $controller->viewPath = 'Errors'; 
     $controller->layout = 'error'; 
     return $controller; 
    } 
} 

這種方法的優點是,它保證了從AppController中拋出的異常渲染異常時不會導致無限循環。每次強制執行異常消息的基本呈現。

9

只需要在/app/View/Errors/error400下的error400.ctp文件中進行佈局更改即可。CTP

打開,通過

<?php $this->layout=''; //set your layout here ?> 
+0

+ +1爲一個非常簡單的解決方案。工作一種享受,幾乎沒有任何努力! (CakePHP 2.4.7) – Hatcham 2014-10-12 23:20:45

+0

這是正確的解決方案。謝謝 – Alireza 2016-05-04 13:05:44