Show symfony2 token username in twig

I am trying to display the username of a registered user in my branch template.

Setting a token that works great with firewalls and logs the user into:

$token = new UsernamePasswordToken($user->getUsername(), $user->getPassword(), "secured_area", $user->getRoles()); $this->get("security.context")->setToken($token); $session->set('_security_secured_area', serialize($token)); 

I want to add something like:

 <div class='meta'>Logged in as <b>username_here</b> 

at the top of my template.

+7
php symfony twig
source share
1 answer

Under normal circumstances, you should be able to display the username by calling this line:

 {{ app.user.username }} 

Just in case, I did not receive the question correctly or did not understand what you have, there is an alternative. Note that was also mentioned in the comments of @Tsounable. This alternative has been deprecated since version 2.6 and should no longer be used!

 {{ app.security.getToken().getUser().getUsername() }} 

Not sure what will work for you. The first is the "standard" way to achieve what you want.

+21
source share

All Articles