29

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

DavidW
  • 5,069
  • 14
  • 46
  • 68

2 Answers2

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
  • 1
    BTW, 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