I am using Doctrine as Auth provider in my symfony2 app. How can I access authenticated user in action or template?
Asked
Active
Viewed 2.0k times
2 Answers
65
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();

Antoine Subit
- 9,803
- 4
- 36
- 52

Herzult
- 3,429
- 22
- 15
-
1BTW, use `$this->container->get('security.authorization_checker')` instead (new in 2.6) – Ronan Aug 30 '15 at 20:34
0
This question is pretty old, but in case anyone is wondering, you can get the user in a template that uses the PHP Engine with
$app->getUser();

diegowc
- 455
- 2
- 14