Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show token username symfony2 in twig

Tags:

php

twig

symfony

I'm trying to display the username of the logged in user in my twig template.

Setting token, which works perfect with firewalls and logs the user in:

            $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 put something like:

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

at the top of my template.

like image 247
skrilled Avatar asked Aug 12 '13 19:08

skrilled


1 Answers

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

{{ app.user.username }} 

Just in case I didn't get the question right or didn't understand what you have there is also an alternative. Note what was also mentioned in the comments by @Tsounable. This alternative is deprecated since version 2.6 and shouldn't be used anymore!

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

Not sure what works out for you. The former is the kind of "standard" way to achieve what you want.

like image 105
SirDerpington Avatar answered Sep 18 '22 22:09

SirDerpington