2013-10-01 19 views
2

我如何處理Kohana 3.3中的所有錯誤? 我的意思是沒有404/505錯誤,但「致命錯誤」從PHP和其他PHP的錯誤?我如何處理Kohana中的所有錯誤3.3

我看着http://kohanaframework.org/3.3/guide/kohana/tutorials/error-pages,我做了這些事情,但它只處理404/505錯誤(和其他人)。我不能處理500錯誤。

創建文件 /APP/Classes/HTTP/Exception/500.php

class HTTP_Exception_500 extends Kohana_HTTP_Exception_500 { 

    public function get_response() 
    { 
     $session = Session::instance(); 
     $view = View::factory('index'); 
      $view->content = View::factory('errors/505'); 
     $view->title = 'Wewnętrzny błąd'; 
     // Remembering that `$this` is an instance of HTTP_Exception_404 
      $view->content->message = 'Wystąpił wewnętrzny błąd. Szczegóły zostały  przekazane do administracji, naprawimy to!'; 

     $response = Response::factory() 
      ->status($this->getCode()) 
      ->body($view->render()); 

     return $response; 
    } 

但它不到風度工作.. 謝謝:)

回答

6

「自定義錯誤頁將只用於處理HTTP_Exception的投擲,如果您只是通過Respose::status()設置404的狀態,則不會使用自定義頁面。「 - 你鏈接的教程。

代碼調用HTTP_Exception::get_response()位於Request_Client_Internal::request_execute()

要處理其他例外,您需要覆蓋Kohana_Exception::response()。像這樣的東西應該工作。

<?php defined('SYSPATH') OR die('No direct script access.'); 

class Kohana_Exception extends Kohana_Kohana_Exception { 

    /** 
    * Generate a Response for all Exceptions without a more specific override 
    * 
    * The user should see a nice error page, however, if we are in development 
    * mode we should show the normal Kohana error page. 
    * 
    * @return Response 
    */ 
    public static function response(Exception $e) 
    { 
     if (Kohana::$environment >= Kohana::DEVELOPMENT) 
     { 
      // Show the normal Kohana error page. 
      return parent::response(); 
     } 

     $view = View::factory('index'); 
     $view->content = View::factory('errors/500'); 
     $view->title = 'Wewnętrzny błąd'; 
     $view->content->message = 'Wystąpił wewnętrzny błąd. Szczegóły zostały przekazane do administracji, naprawimy to!'; 

     $response = Response::factory() 
      ->status(500) 
      ->body($view->render()); 

     return $response; 
    } 
} 
0

你可以只寫這個片段在bootstrap.php中

if (Kohana::$environment == Kohana::PRODUCTION) 
{ 
    Kohana_Exception::$error_view = 'template/errors'; 
} 

,你仍然可以有HTTP_Exceptions

<?php defined ('SYSPATH') or die ('No direct script access.'); 

class HTTP_Exception extends Kohana_HTTP_Exception { 

    /** 
    * Generate a Response for all Exceptions without a more specific override 
    * 
    * The user should see a nice error page, however, if we are in development 
    * mode we should show the normal Kohana error page. 
    * 
    * @return Response 
    */ 
    public function get_response() 
    { 
     // Lets log the Exception, Just in case it's important! 
     Kohana_Exception::log($this); 

     if (Kohana::$environment >= Kohana::DEVELOPMENT) 
     { 
      // Show the normal Kohana error page. 
      return parent::get_response(); 
     } 
     else 
     { 
      // Generate a nicer looking "Oops" page. 
      $view = View::factory('template/http_errors'); 

      $response = Response::factory() 
       ->status($this->getCode()) 
       ->body($view->render()); 

      return $response; 
     } 
    } 
} 
有不同的看法