Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing logged in user in Symfony2 action and template

Tags:

symfony

I am using Doctrine as Auth provider in my symfony2 app. How can I access authenticated user in action or template?

like image 570
DavidW Avatar asked Dec 18 '11 09:12

DavidW


1 Answers

In your templates, you can do:

{{ app.user }} 

And in your controller, if you extend the base controller provided by the framework bundle, you can do:

$this->getUser(); 

Anyway, you can access it from the service container:

$securityContext = $container->get('security.context'); $token = $securityContext->getToken(); $user = $token->getUser(); 
like image 190
Herzult Avatar answered Sep 27 '22 19:09

Herzult