当我覆盖404错误页面时,app.request.attributes.get('_ route')为空

I'm trying to customize 404 error page so I did:

1) Create app/Resources/TwigBundle/views/Exception/error404.html.twig

2) I changed is_granted() for {% if app.user and is_granted('...') %} in "layout.html.twig", used by "error404.html.twig"

3) php app/console cache:clear --env=prod --no-debug

4) Access in prod enviroment to http:/localhost/app.php and cause 404 error, getting a blank page instead of my custom error page.

I checked my "prod.log" file and I saw a NotFoundHttpException and this Twig error:

request.ERROR: Exception thrown when handling an exception (Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "" as such route does not exist.") in "::layout.html.twig" at line 33.) [] []

It appears that path(app.request.attributes.get('_route')) returns an empty value.

-> How can I get current route and make error page work?<-

Thanks

As Maerlyn says in his comment, it's likely that, if you're on a 404 page, there is no matching route.

However, you can retrieve useful information from the request object which may help you here:

{{ app.request.uri }}

will return you the full URL of the page the user was trying to access. And

{{ app.request.pathInfo }}

will return the path (starting with a slash).

You can find out more about the methods available on the request object by looking at /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Request.php.