Symfony2 error 500 instead of 404 at production Symfony2 error 500 instead of 404 at production symfony symfony

Symfony2 error 500 instead of 404 at production


Symfony\Component\Routing\Exception\ResourceNotFoundException means undefined route name. It looks like you've got somewhere in your error template {{ path('wrong_route') }}.


The most likely reason you're getting a 500 error / blank page on production (app.php), even when you've defined a custom error page ( e.g. app/Resources/TwigBundle/views/Exception/error404.html.twig ) is that your error template is calling the is_granted twig function, without checking if the user is logged in.

Steps to debug:

1)Check app/logs/prod.log. Do you see an error like this?

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.")

2)If you see the error mentioned above, see if you can find a reference to is_granted in your error template. Check that the user is logged in before calling is_granted. E.g.:

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

3)If you can't find a reference to is_granted in your template, see if there's a call to knp_menu_render(), which is used by the KNP menu bundle. Check in any extended template as well. Wrap the call to knp_menu_render in a check to verify that the user is logged in:

{% if app.user %}    {{ knp_menu_render() }}{% endif %}

For more information, check the comment by stof at the end of this page:https://github.com/symfony/symfony/issues/5320


In my case it was use of {% stylesheets %} tag in a 404 template, while TwigBundle was not included in the Assetic config.

Check your app/logs/prod.log, it should have an answer.