Question

I have seen so far a Twig method to know the user privilege

{% if is_granted('ROLE_USER') %}

but nothing about getting authentified user informations, without passing the object in every single controller actions.

Before looking for how to pass a global variable to my layout, I'd rather know if there is any "standard" way to access my security provider user objects (at least the username, and database informations for entity users) in my layout or in any template covered by a firewall.

Was it helpful?

Solution

Try {{ app.user }}.

The app template variable provides you access to a lot of the global variables that you'd expect, like request, session, security, etc. Check out the source code for more info.

One thing you should be aware of is that while the GlobalVariables class provides getters for all of the variables, Twig is smart enough to know how to deal with the short form, so that {{ app.user }} is equivalent to {{ app.getUser() }}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top