Kohana_Request_Exception [0]:无法找到与 URI 匹配的路由:

发布于 2024-11-03 10:57:53 字数 265 浏览 3 评论 0原文

我有一个 kohana 网站,我想将其投入生产,但我有一个问题,我不知道如何最好地解决: 如果有人访问相对于我的网站而言不存在的 uri,则会出现一个错误页面,并带有标题消息:

Kohana_Request_Exception [0]:无法 查找与 URI 匹配的路由:(以及此处的 uri)

我想知道当用户访问这样的 URI 时,我是否可以做一些事情将用户重定向到标准 404 页面,可以吗?

非常感谢!

i have a kohana site and i want to put it into production, but i have a problem i don't know how to solve best:
if someone accesses a uri that doesn't exist, relatively to my website, an error page appears, with the header message:

Kohana_Request_Exception [ 0 ]: Unable
to find a route to match the URI: (and the uri here)

i wonder if I can do something to redirect the user to a standard 404 page, when he/she accesses such a URI,can I?

thank you very much!

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

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

发布评论

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

评论(3

唯憾梦倾城 2024-11-10 10:57:53

您可以使用 try/catch 块将 $request->execute() 包装在 APPPATH/bootstrap.php 中,然后执行您想做的任何操作。

我的看起来像这样...

try 
{
    // Attempt to execute the response
    $request->execute();
}
catch (Kohana_Request_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('errors/404')->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 500 response
    $request->status = 500;

    $request->response = Request::factory('errors/500')->execute();
}

理想情况下 PHP 将支持 finally { ... } 并且我可以在那里进行日志记录和可能的重新抛出,但是你能做什么?

You can wrap your $request->execute() in your APPPATH/bootstrap.php with a try/catch block and then do whatever you want.

Mine looks like this...

try 
{
    // Attempt to execute the response
    $request->execute();
}
catch (Kohana_Request_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('errors/404')->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 500 response
    $request->status = 500;

    $request->response = Request::factory('errors/500')->execute();
}

Ideally PHP would support finally { ... } and I could do the logging and possible re-throwing there, but what can you do?

漫雪独思 2024-11-10 10:57:53

您可以按照 http 异常用户指南中描述的方式处理所有错误:

http://kohanaframework.org/3.1/guide/kohana/errors#http-exception-handling

You can handle all errors the same way it is described in the userguide for http exceptions:

http://kohanaframework.org/3.1/guide/kohana/errors#http-exception-handling

記憶穿過時間隧道 2024-11-10 10:57:53

试试这个

    define('IN_PRODUCTION', TRUE);
 // Instantiate your Request object
    $request = Request::instance();
    try
    {
        $request->execute();
    }
    catch (Exception $e) // if its not valid, it gets caught here
    {
        if (! IN_PRODUCTION) // if this is Development, its displays the error
        {
            throw $e;
        }
        // if its IN_PRODUCTION, it does the following:
        // Logs the error
        Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
        // Marks the status as 404
        $request->status = 404;
         $request->response = $request->factory('sitemap')->execute();
    }
    // then continues on with the request process to display your custom 404 page
    $request->send_headers()->response;
    echo $request->response;

Try out this one

    define('IN_PRODUCTION', TRUE);
 // Instantiate your Request object
    $request = Request::instance();
    try
    {
        $request->execute();
    }
    catch (Exception $e) // if its not valid, it gets caught here
    {
        if (! IN_PRODUCTION) // if this is Development, its displays the error
        {
            throw $e;
        }
        // if its IN_PRODUCTION, it does the following:
        // Logs the error
        Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
        // Marks the status as 404
        $request->status = 404;
         $request->response = $request->factory('sitemap')->execute();
    }
    // then continues on with the request process to display your custom 404 page
    $request->send_headers()->response;
    echo $request->response;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文