如何在Kohana中捕获HTTP_Exception_404错误

时间:2011-09-16 08:18:36

标签: 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
        • 控制器
          • 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

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

5 个答案:

答案 0 :(得分:2)

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

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

您提到直接转到错误处理程序控制器的url会触发404错误会让我觉得您的模块尚未加载。

我还建议进行一些更改。

http_response_exception.php不需要扩展Kohana_Exception,因为这个类不是异常,而是异常处理程序。沿着这些相同的行,更合适的类名可能是Exception_Handler,因为该类不表示异常,而是处理它们。其次,由于你如何命名这个文件,它应该位于modules / my / classes / http / response / exception.php中。除此之外,这个类的代码看起来还不错。

同样,由于你如何命名你的控制器,它的位置和命名应该有点不同。将其移至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;
    }
}

}

答案 1 :(得分:2)

您使用的是过时的文档。 HTTP_Exception_404捆绑在3.1中,您正在尝试从3.0实现解决方案。

有关可行的解决方案,请参阅documentation for your version of Kohana

答案 2 :(得分:2)

您需要做的就是在bootstrap.php add中设置不同视图的路径:

Kohana_Exception::$error_view = 'error/myErrorPage';

将解析当前正在解析的所有变量到生成的错误页面:

system/views/kohana/error.php

即:

<h1>Oops [ <?= $code ?> ]</h1>
<span class="message"><?= html::chars($message) ?></span>

答案 3 :(得分:2)

非常长时间搜索之后,我终于找到了解决我的小问题的方法。

以下是有关如何使用Kohana 3.2加载自定义错误页面的分步教程:

  1. 更改引导程序中的环境变量。
  2. 这里有多种选择:

    一个。在documentation of the bootstrap.php

    中做他们所说的话
    /**
     * Set the environment status by the domain.
     */
    
    if (strpos($_SERVER['HTTP_HOST'], 'kohanaphp.com') !== FALSE)
    {
        // We are live!
        Kohana::$environment = Kohana::PRODUCTION;
    
        // Turn off notices and strict errors
        error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
    }
    

    湾或者只添加这两行而不使用“if”:

    Kohana::$environment = Kohana::PRODUCTION;
    error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
    

    ℃。我没有尝试这种方式,但在新的bootstrap.php中你有这个代码:

    /**
     * Set Kohana::$environment if a 'KOHANA_ENV' environment variable has been supplied.
     *
     * Note: If you supply an invalid environment name, a PHP warning will be thrown
     * saying "Couldn't find constant Kohana::<INVALID_ENV_NAME>"
     */
    if (isset($_SERVER['KOHANA_ENV']))
    {
        Kohana::$environment = constant('Kohana::'.strtoupper($_SERVER['KOHANA_ENV']));
    }
    

    我认为您可以在这些行之前将值“production”赋予“$ _SERVER ['KOHANA_ENV']”。

    同样,就像我说的那样,我没有尝试过,但它应该有用。

    我个人刚刚评论了这些代码行。

    2现在,您需要在“ini.php”文件或“bootstra.php”文件中添加一些配置。

    <?php defined('SYSPATH') or die('No direct script access.');
    
    /**
     * Turn errors into exceptions. 
     */
    Kohana::$errors = true;
    
    /**
     * Custom exception handler.
     */
    restore_exception_handler();
    set_exception_handler(array('Exception_Handler', 'handler'));
    
    /**
     * Error route.
     */
    Route::set('error', 'error/<action>(/<message>)', array('action' => '[0-9]++', 'message' => '.+'))
    ->defaults(array(
        'controller' => 'exception_handler'
    ));
    

    这就是缺少的东西,并使其变得艰难。其余的你可以轻松地按照Kohana3.2文档,或者你可以获得我在GitHub中添加到repo的模块:https://github.com/jnbdz/Kohana-error

答案 4 :(得分:0)

每个下划线都是类名中的目录分隔符。因此,在命名您的班级Http_Response_Exception时,该班级应位于classes/http/response/exception.php。否则,Kohana的自动加载器将无法找到该类。

修改

嗯,好像文档在这方面是错误的。 classes/http_response_exception.php没有意义。