I'm trying to get the username of logged user in the 404 error page, like the symfony docs says;
{# app/Resources/TwigBundle/views/Exception/error404.html.twig #}
{% extends 'base.html.twig' %}
{% block body %}
<h1>Page not found</h1>
{# example security usage, see below #}
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
IS_AUTHENTICATED_FULLY: {{ app.user.username }}
{% endif %}
{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
IS_AUTHENTICATED_REMEMBERED: {{ app.user.username }}
{% endif %}
{% if app.user %}
app.user: {{ app.user.username }}
{% endif %}
<p>
The requested page couldn't be located. Checkout for any URL
misspelling or <a href="{{ path('homepage') }}">return to the homepage</a>.
</p>
{% endblock %}
Everything works in dev, but in production the user is always not logged. Every if condition fails.
In this discussion I found this:
The cause of this problem is that routing is done before security. If a 404 error occurs, the security layer isn't loaded and thus
So... can be possible to get the logged user in twig exception page?
Update
It looks like it is a desired behavior: https://github.com/symfony/symfony/issues/8414#issuecomment-23661839
This hack can "solve" but it is really ugly...