27
votes

In my Symfony2 project I'm getting at development mode correct 404 Exception screen. But I'm getting blank screen with HTTP status code 500 instead of 404 at production mode. I'm using custom error templates located in app/Resources/TwigBundle/views/Exception. In apache error log it creates this message:

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
11
I've encountered a similar problem. How did you resolve this?Marijn Huizendveld

11 Answers

15
votes

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

10
votes

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

5
votes

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.

4
votes

ResourceNotFoundException is what the router throw when no route match the current request.

This can be a cache issue (80% of the time, it's the cache. Try rm -rf app/cache/* on your pre-production server). As the issue does not appear locally... (you tried the "prod" env locally right?).

You should also try to remove everything from your app/Resources/TwigBundle/views/Exception/error404.html.twig (is that the filename you use?) except simple HTML, to check if it's not a twig issue.

3
votes

I had the same issue,

But my isGranted was in the php side. So I added a token check :

Before :

if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')

After :

if ($this->get('security.token_storage')->getToken() && $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')
2
votes

Here is how I redirect all non existent routes to root path. Put this route entry on the very bottom of your routing configuration. All existent route MUST be on top of it!

anything:
    path:     /{path}
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /
        permanent: true
    requirements:
        path: ".+"

reference:
http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html
http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html

1
votes

Another option is that you're trying to access the security token in the security context when no token is available.

0
votes

maybe app/Resources/TwigBundle/views/Exception/error.html.twig references another base-layout-template which does not exists - this was the problem in my case

0
votes

In my case (symfony 2.3) I was getting a 200 status code, so the error pages didn't load. That's how I solved it:

https://groups.google.com/d/msg/Symfony2/zNWNmQIq3nk/lVs4uC7QMIcJ

0
votes

Just an complementary note to the given answers: The ResourceNotFoundException can also be thrown if you are trying to include a non existent template. A typical case, is that you refactored your application code but you forgot to update your error pages as they are located in app/Resources and not in your src/ folder or including a given template that use a variable or service that is not defined in the "error" context.

0
votes

I encounteres that Symofny 3 throws an 500 error in production when you do not use

$this->createNotFoundException()

in your Controller.

throw Exception('message', 404)

works on dev-enviroment, but not on production.