【问题标题】:Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI:Kohana_Request_Exception [ 0 ]:找不到匹配 URI 的路由:
【发布时间】:2011-04-25 06:11:29
【问题描述】:

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

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

我想知道当他/她访问这样的 URI 时,我是否可以将用户重定向到标准 404 页面,可以吗?

非常感谢!

【问题讨论】:

  • 请出示您的 Kohana 版本 (3.0.x/3.1.x)。由于请求 API 更改,它很重要。
  • 我已经阅读了您链接的页面的内容,但这完全让我感到困惑。我需要一个适用于我所有页面的解决方案,而不是单独适用于每个控制器。
  • 本教程向您展示如何使用其自定义处理程序 (Error::exception_handler(Exception $e)) 和 one 特殊错误控制器 (Controller_Error_Handler) 捕获所有异常
  • 检查 redirect 是否适合你 dana。

标签: exception controller kohana uri


【解决方案1】:

您可以使用 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 { ... },我可以在那里进行日志记录和可能的重新抛出,但你能做什么?

【讨论】:

  • 我试过这样,但它使我的网站崩溃。它首先给了我一个错误: ErrorException [ Notice ]: Undefined index: user_id 然后我应该删除浏览器历史记录和上面的代码行,以便恢复它。我想知道为什么。
  • 如果我只是把上面的代码放在引导程序中,我得到:未定义的变量:请求
  • 您需要将实例分配给它。
  • @dana $request = Request::instance().
  • 我已将其添加到引导文件的末尾,但没有解决。如果我访问相对于我的网站的随机 url,我仍然会收到该错误: Kohana_Request_Exception [ 0 ]: Unable to find a route to match the URI 而不是进入自定义 404 页面
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    试试这个

        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;
    

    【讨论】:

      猜你喜欢
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-01-14
      • 1970-01-01
      • 2019-08-27
      • 2019-02-22
      • 1970-01-01
      • 2020-01-08
      相关资源
      最近更新 更多