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

发布于 2024-09-02 18:21:16 字数 1867 浏览 10 评论 0原文

基于此文档,我实现了一个catch all 路由到错误页面。

这是我的 bootstrap.php 中的最后一条路线

Route::set('default', '<path>', array('path' => '.+'))
    ->defaults(array(
        'controller' => 'errors',
        'action'     => '404',
    ));

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

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

如果我将 段设置为可选(即将其括在括号中),那么它似乎只是加载 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 错误页面?

Based on this documentation, I've implemented a catch all route which routes to an error page.

Here is the last route in my bootstrap.php

Route::set('default', '<path>', array('path' => '.+'))
    ->defaults(array(
        'controller' => 'errors',
        'action'     => '404',
    ));

However I keep getting this exception thrown when I try and go to a non existent page

Kohana_Exception [ 0 ]: Required route
parameter not passed: path

If I make the <path> segment optional (i.e. wrap it in parenthesis) then it just seems to load the home route, which is...

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

The home route is defined first.

I execute my main request like so

$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;

This means that the 404 header is sent to the browser, but I assumed by sending the request to the capture all route then it should show the 404 error set up in my errors controller.

<?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;


    }
}

Why won't it show my 404 error page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城 2024-09-09 18:21:16

对于我的项目,我从未为 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;
}

例外404:

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

For my projects I never specified any specific separated route for 404. I just catch exceptions thrown by routing, when no appropriate route was found.

Here is how I am handling all kind of exceptions that I've discovered in Kohana. I hope it will help you.

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);
    }
 }
下雨或天晴 2024-09-09 18:21:16

啊,我想出了这个。

这一行...

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

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

Ah, I figured this one out.

This line...

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

Was setting the response to the result of Route::get('default')->uri(). Looking at it further, I realised it will return an empty string, which will match the home route above. I feel silly....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文