为什么“找不到路线”在Silex中有未被捕获的例外?

时间:2013-05-30 15:56:24

标签: php symfony twig silex

当用户尝试不存在的路由时,我的网站正在记录未捕获的异常。日志看起来像这样:

[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));
    }

});

1 个答案:

答案 0 :(得分:3)

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

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

如果要在操作中强制使用404,则显式调用NotFoundHttpException!