生产时Symfony2错误500而不是404

kub*_*lav 26 symfony

在我的Symfony2项目中,我正处于开发模式正确的404 Exception屏幕.但是我在生产模式下使用HTTP状态代码500而不是404获取空白屏幕.我正在使用自定义错误模板app/Resources/TwigBundle/views/Exception.在apache错误日志中,它会创建以下消息:

PHP Fatal error:  Uncaught exception 'Symfony\\Component\\Routing\\Exception\\ResourceNotFoundException' in /home/test/app/cache/prod/appprodUrlMatcher.php:518\nStack trace:
#0 /home/test/app/cache/prod/classes.php(1025): appprodUrlMatcher->match('/404')
#1 /home/test/app/cache/prod/classes.php(4550): Symfony\\Component\\Routing\\Router->match('/404')
#2 [internal function]: Symfony\\Bundle\\FrameworkBundle\\EventListener\\RouterListener->onKernelRequest(Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#3 /home/test/app/cache/prod/classes.php(3777): call_user_func(Array, Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#4 /home/test/app/cache/prod/classes.php(3703): Symfony\\Component\\EventDispatcher\\EventDispatcher->doDispatch(Array, 'kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent))
#5 /home/test/app/cache/prod/classes.php(4787): Symfony\\Component\\EventDispatcher\\EventDispatcher->dispatch('kernel.request', Object(Symfony\\Component\\HttpKernel\\Event\\Get in /home/test/app/cache/prod/classes.php on line 4560
Run Code Online (Sandbox Code Playgroud)

jku*_*vic 15

Symfony\Component\Routing\Exception\ResourceNotFoundException表示未定义的路由名称.看起来你的错误模板中有一个地方{{ path('wrong_route') }}.

  • 我有类似的东西,但它是由`is_granted('IS_AUTHENTICATED_FULLY')`引起的 - 只有我的404页面,我的403页面加载得很好.对此有何见解?[链接](http://stackoverflow.com/questions/11869921/symfony2-is-grantedis-authenticated-fully-during-404-error-page-display-cau) (4认同)

Jay*_*eth 9

即使您已经定义了自定义错误页面(例如app/Resources/TwigBundle/views/Exception/error404.html.twig),您最有可能在生产(app.php)上获得500错误/空白页面的原因是您的错误模板正在调用is_granted twig函数,而不检查用户是否已登录.

调试步骤:

1)检查app/logs/prod.log.你看到这样的错误吗?

request.ERROR: Exception thrown when handling an exception (Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.")
Run Code Online (Sandbox Code Playgroud)

2)如果您看到上述错误,请查看是否可以在错误模板中找到对is_granted的引用.在调用is_granted之前检查用户是否已登录.例如:

{% if app.user is not null and is_granted('ROLE_ADMIN') %}
<p>Text goes here</p>
{% else %}
Run Code Online (Sandbox Code Playgroud)

3)如果在模板中找不到is_granted的引用,请查看是否有对KNP菜单包使用的knp_menu_render()的调用.检查任何扩展模板.在检查中包含对knp_menu_render的调用,以验证用户是否已登录:

{% if app.user %}
    {{ knp_menu_render() }}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看本页末尾的stof注释:https: //github.com/symfony/symfony/issues/5320


Kon*_*lov 5

在我的例子中,在404模板中使用了{%stylesheets%}标签,而在Assetic配置中没有包含TwigBundle.

检查一下app/logs/prod.log,它应该有一个答案.