php - 如何在 Kohana 中捕获 HTTP_Exception_404 错误

标签 php error-handling kohana http-error

我尝试按照此处的说明进行操作:http://kohanaframework.org/3.0/guide/kohana/tutorials/error-pages但由于某种原因,我无法捕捉到 HTTP_Exception_404 我仍然得到一个丑陋的错误页面,而不是我的自定义页面。

此外,当我输入 URL error/404/Message 时,我会收到一条丑陋的 Kohana HTTP 404 错误消息。

这是文件结构:

  • 模块
    • 我的
      • init.php
        • Controller
          • error_handler.php
        • http_response_exception.php
        • kohana.php
      • 观看次数
        • error.php

代码:

init.php:

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

Route::set('error', 'error/<action>(/<message>)', array('action' => '[0-9]++', 'message' => '.+'))
    ->defaults(array(
            'controller' => 'error_handler'
));

http_response_exception.php:

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

class HTTP_Response_Exception extends Kohana_Exception {


    public static function exception_handler(Exception $e)
    {

            if (Kohana::DEVELOPMENT === Kohana::$environment)
            {
                    Kohana_Core::exception_handler($e);
            }
            else
            {
                    Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

                    $attributes = array
                    (
                            'action'  => 500,
                            'message' => rawurlencode($e->getMessage()),
                    );

                    if ($e instanceof HTTP_Response_Exception)
                    {
                            $attributes['action'] = $e->getCode();
                    }

                    // Error sub-request.
                    echo Request::factory(Route::url('error', $attributes))
                            ->execute()
                            ->send_headers()
                            ->response;
            }
    }
}

kohana.php:

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

class Kohana extends Kohana_Core
{

    /**
     * Redirect to custom exception_handler
     */
    public static function exception_handler(Exception $e)
    {
            Error::exception_handler($e);
    }

} // End of Kohana

error_handler.php:

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

class Controller_Error_handler extends Controller {

    public function  before()
    {
            parent::before();

            $this->template = View::factory('template/useradmin');
            $this->template->content = View::factory('error');

            $this->template->page = URL::site(rawurldecode(Request::$instance->uri));

            // Internal request only!
            if (Request::$instance !== Request::$current)
            {
                    if ($message = rawurldecode($this->request->param('message')))
                    {
                            $this->template->message = $message;
                    }
            }
            else
            {
                    $this->request->action = 404;
            }
    }

    public function action_404()
    {
            $this->template->title = '404 Not Found';

            // Here we check to see if a 404 came from our website. This allows the
            // webmaster to find broken links and update them in a shorter amount of time.
            if (isset ($_SERVER['HTTP_REFERER']) AND strstr($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) !== FALSE)
            {
                    // Set a local flag so we can display different messages in our template.
                    $this->template->local = TRUE;
            }

            // HTTP Status code.
            $this->request->status = 404;
    }

    public function action_503()
    {
            $this->template->title = 'Maintenance Mode';
            $this->request->status = 503;
    }

    public function action_500()
    {
            $this->template->title = 'Internal Server Error';
            $this->request->status = 500;
    }

} // End of Error_handler

我真的看不出我哪里做错了。提前感谢您的帮助。

最佳答案

首先,您需要确保正在加载您的模块,方法是将其包含在 application/bootstrap.php 文件的模块部分中,如下所示

Kohana::modules(array(
'my'=>MODPATH.'my'
)
);

您提到直接访问错误处理程序 Controller 的 url 会触发 404 错误,这让我认为您的模块尚未加载。

我还建议进行更多更改。

http_response_exception.php 不需要扩展 Kohana_Exception,因为这个类不是异常,而是异常处理程序。按照同样的思路,更合适的类名可能是 Exception_Handler,因为该类不代表异常,而是处理它们。其次,由于您命名此文件的方式,它应该位于 modules/my/classes/http/response/exception.php 中。除此之外,这个类的代码看起来没问题。

同样,由于您对 Controller 的命名方式,它的定位和命名方式应该有所不同。将其移至 modules/my/classes/controller/error/handler.php

请记住,根据 http://kohanaframework.org/3.2/guide/kohana/conventions,类名中的下划线表示新目录

最后,我认为您不需要在此处扩展 Kohana_Core 类,只需注册您自己的自定义异常处理程序即可。您可以在应用程序的引导文件或模块的 init 文件中使用以下通用代码注册自定义异常处理程序:

set_exception_handler(array('Exception_Handler_Class', 'handle_method'));

这是我使用的客户异常处理程序,与您的非常相似:

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

class Exception_Handler {

public static function handle(Exception $e)
{
    $exception_type = strtolower(get_class($e));
    switch ($exception_type)
    {
        case 'http_exception_404':
            $response = new Response;
            $response->status(404);
            $body = Request::factory('site/404')->execute()->body();
            echo $response->body($body)->send_headers()->body();
            return TRUE;
            break;
        default:
            if (Kohana::$environment == Kohana::DEVELOPMENT)
            {
                return Kohana_Exception::handler($e);
            }
            else
            {
                Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e));
                $response = new Response;
                $response->status(500);
                $body = Request::factory('site/500')->execute()->body();
                echo $response->body($body)->send_headers()->body();
                return TRUE;
            }
            break;
    }
}

}

关于php - 如何在 Kohana 中捕获 HTTP_Exception_404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7441907/

相关文章:

mysql - Kohana 3.2 选择问题

php - EasyPHP win7 服务器上的 Kohana Web 应用程序中的 SQL 查询速度极慢

javascript - Node.js 数据模型对象语法

php - Silverstripe 过滤器关系 AND 代替 OR

javascript - 脚本和样式未在 functions.php 中排队

php - PHP : REQUEST_METHOD as basic error handling for html form

php - 如何使用位域存储星期几?

php - SQLSTATE [HY000] : General error

java - 安全异常 : Unable to find field for dex. jar android

php - Kohana 验证 : correct syntax for range rule