0

当用户尝试不存在的路由时,我的站点正在记录未捕获的异常。日志如下所示:

[2013-05-30 15:47:38] myapp.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /a/made/up/url" (uncaught exception) at /home/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/EventListener/RouterListener.php line 92 [] []

这是正常的吗?或者有什么方法可以让我捕捉到异常?

我的错误处理代码如下所示:

$app->error(function (\Exception $e, $code) use ($app) {

    $app['monolog']->addDebug($e);

    // Some app-specific code
    $globals = $app['select_globals']();

    switch ($code) {
        case 404:
            return $app['twig']->render('404.phtml', array('globals' => $globals));
            break;
        default:
            return $app['twig']->render('error.phtml', array('globals' => $globals));
    }

});
4

1 回答 1

3

是的,这完全正常,Symfony2(还有 Silex)记录异常,未找到的页面被视为异常!

查看此文档:http ://symfony.com/doc/current/book/controller.html#managing-errors-and-404-pages

当您想在您的操作中强制执行 404 时,您正在显式调用 NotFoundHttpException !

于 2013-05-30T16:01:22.323 回答