27

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
kuboslav
  • 1,430
  • 5
  • 16
  • 31

11 Answers11

15

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

jkucharovic
  • 4,214
  • 1
  • 31
  • 46
  • 5
    I have something similar going on, but it is being caused by `is_granted('IS_AUTHENTICATED_FULLY')` - and only for my 404 page, my 403 page loads just fine. Any insight on that? [link](http://stackoverflow.com/questions/11869921/symfony2-is-grantedis-authenticated-fully-during-404-error-page-display-cau) – Nick Aug 08 '12 at 17:39
  • 1
    @Routy you got [the answer](https://github.com/symfony/symfony/issues/5225#issuecomment-7626407) but forgot to mention it here :) – coviex Jun 25 '15 at 18:56
10

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

Jay Sheth
  • 1,738
  • 16
  • 15
5

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.

Konstantin Pereiaslov
  • 1,786
  • 1
  • 18
  • 26
4

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.

Damien
  • 5,872
  • 2
  • 29
  • 35
3

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')
Gautier
  • 1,066
  • 1
  • 13
  • 24
2

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

ihsan
  • 2,279
  • 20
  • 36
  • There must be something wrong with SO. I remember this answer, but definitely not for this question! – ihsan May 10 '15 at 08:57
1

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

Ivanka Todorova
  • 9,964
  • 16
  • 66
  • 103
Marijn Huizendveld
  • 791
  • 2
  • 7
  • 23
0

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

Stefan
  • 1
0

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

devilcius
  • 1,764
  • 14
  • 18
0

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.

COil
  • 7,201
  • 2
  • 50
  • 98
0

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.

Sebastian Viereck
  • 5,455
  • 53
  • 53