2

我如何处理 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;
    }

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

4

2 回答 2

6

“自定义错误页面将仅用于处理 throw 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;
    }
}
于 2013-10-01T12:11:24.580 回答
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;
        }
    }
}
于 2014-09-17T16:50:45.870 回答