需要协助Kohana 3并捕获所有路线变成404错误

时间:2010-05-19 01:23:10

标签: php kohana kohana-3

基于this documentation,我实现了一个 catch all 路由,路由到错误页面。

以下是我bootstrap.php

中的最后一条路线
Route::set('default', '<path>', array('path' => '.+'))
    ->defaults(array(
        'controller' => 'errors',
        'action'     => '404',
    ));

但是,当我尝试转到不存在的页面时,我不断抛出此异常

  

Kohana_Exception [0]:必需的路线   参数未传递:路径

如果我将<path>段设为可选(即将其括在括号中),那么它似乎只是加载了home路径,这是......

Route::set('home', '')
    ->defaults(array(
        'controller' => 'home',
        'action'     => 'index',
    ));

首先定义归属路由。

我执行我的主要请求

$request = Request::instance();


try {

    // Attempt to execute the response
    $request->execute();

} catch (Exception $e) {


   if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;

    // Log the error
    Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

    // Create a 404 response
    $request->status = 404;
    $request->response = Request::factory(Route::get('default')->uri())->execute();

}

$request->send_headers();
echo $request->response;

这意味着404标头被发送到浏览器,但我假设通过将请求发送到捕获所有路由,那么它应该显示我的错误控制器中设置的404错误。

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

class Controller_Errors extends Controller_Base {

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

    public function action_404() {


        $this->bodyClass[] = '404';

        $this->internalView = View::factory('internal/not_found');

        $longTitle = 'Page Not Found';

        $this->titlePrefix = $longTitle;


    }
}

为什么不显示我的404错误页面?

2 个答案:

答案 0 :(得分:1)

对于我的项目,我从未为404指定任何特定的分隔路由。我只是捕获路由引发的异常,当找不到合适的路由时。

以下是我如何处理我在Kohana中发现的各种异常。我希望它会对你有所帮助。

try
{
    try
    {
        $request = Request::instance();
        $request->execute();
    }
    catch (ReflectionException $e)
    {
        Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        $request->response = Request::factory('err/404')->execute();
    }
    catch (Exception404 $e)
    {
        Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        $request->response = Request::factory('err/404')->execute();
    }
    catch (Kohana_Request_Exception $e)
    {
        Kohana::$log->add(Kohana::ERROR_404, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, 404);
        echo Request::factory('err/404')->send_headers()->execute()->response;
        exit;
    }
    catch (exception $e)
    {
        Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

        if (!IN_PRODUCTION)
        {
        throw $e;
        }

        $request->status = 500;
        $request->response = Request::factory('err/500')->execute();
    }
}
catch (exception $e)
{
        if (!IN_PRODUCTION)
        {
            throw $e;
        }
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        </head>
        <body>
        here is the message that everything is veeeery bad
        </body>
        </html>';
        exit;
}

Exception404:

class Kohana_Exception404 extends Kohana_Exception
{
    public function __construct($message = 'error 404', array $variables = NULL, $code = 0)
    {
        parent::__construct($message, $variables, $code);
    }
 }

答案 1 :(得分:0)

啊,我想出了这个。

这一行...

$request->response = Request::factory(Route::get('default')->uri())->execute();

将响应设置为Route::get('default')->uri()的结果。进一步看,我意识到它将返回一个空字符串,它将匹配上面的home路由。我觉得很傻....