我如何处理Kohana 3.3中的所有错误

时间:2013-10-01 00:06:50

标签: php kohana

我如何处理Kohana 3.3中的每个错误? 我的意思是没有404/505错误但是来自php和其他php的错误的“致命错误”?

我查看了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;
    }

但它不起作用.. 谢谢:))

2 个答案:

答案 0 :(得分:6)

“自定义错误页面仅用于处理投掷HTTP_Exception。如果您只是通过Respose::status()设置状态,则不会使用自定义页面。” - 您链接的教程。

调用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;
    }
}

答案 1 :(得分: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;
        }
    }
}