101

I'm using FOSuserbundle to get started with User registration https://github.com/FriendsOfSymfony/FOSUserBundle

I've got it registering / logging in and out. What I want to do now is grab the logged in users data and present it on every page of my site. Like "Hi username" in the header type of thing.

It seems like embedding a controller in my app/Resources/views/base.html.twig is the best way to do this http://symfony.com/doc/current/book/templating.html#embedding-controllers

So I wrote my controller to access the user profile data. What I can't figure out is how to access FOS methods in my embedded controller. So from my Acme/UserBundle/Controller/UserController.php I want to do this:

public function showAction()
{
    $user = $this->container->get('security.context')->getToken()->getUser();
    if (!is_object($user) || !$user instanceof UserInterface) {
        throw new AccessDeniedException(
               'This user does not have access to this section.');
    }

    return $this->container->get('templating')
      ->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container
      ->getParameter('fos_user.template.engine'), array('user' => $user));
}

which I grabbed from: vendor/bundles/FOS/UserBundle/Controller/ProfileController.php

ed209
  • 11,075
  • 19
  • 66
  • 82

3 Answers3

237

You can access user data directly in the twig template without requesting anything in the controller. The user is accessible like that : app.user.

Now, you can access every property of the user. For example, you can access the username like that : app.user.username.

Warning, if the user is not logged, the app.user is null.

If you want to check if the user is logged, you can use the is_granted twig function. For example, if you want to check if the user has ROLE_ADMIN, you just have to do is_granted("ROLE_ADMIN").

So, in every of your pages you can do :

{% if is_granted("ROLE") %}
    Hi {{ app.user.username }}
{% endif %}
egeloen
  • 5,844
  • 1
  • 27
  • 38
  • 8
    thanks. I actually changed "ROLE" for "IS_AUTHENTICATED_REMEMBERED" and it worked great. – ed209 Sep 27 '11 at 19:42
  • 5
    I'm not using FOSuserbundle (unless it now comes with) but `app.user.username` still worked for me. Might be useful for somebody to know. – Jason Swett Mar 30 '12 at 02:52
  • 1
    Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables is not part of the FOSUserBundle which is why this works always (that's where the twig Globals are defined in 2.5) (with regards to Jason Swett Mar 30') – DerStoffel Apr 22 '15 at 12:00
18

For symfony 2.6 and above we can use

{{ app.user.getFirstname() }}

as app.security global variable for Twig template has been deprecated and will be removed from 3.0

more info:

http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

and see the global variables in

http://symfony.com/doc/current/reference/twig_reference.html

zubair Shaik
  • 395
  • 6
  • 16
  • 4
    unnecessary method call. You can use dynamic attributes like this `{{ app.user.firstname }}`. It's more front-end-dev-friendly I guess. – kacper3w Dec 31 '15 at 13:27
  • 2
    Even though I agree with the comment above about being unnecessary, I'm going to upvote this because it demonstrates that custom methods can be used if so desired – Simon Mar 29 '16 at 13:03
-2
{{ app.user.username|default('') }}

Just present login username for example, filter function default('') should be nice when user is NOT login by just avoid annoying error message.

Wei Zhang
  • 24
  • 4
  • 2
    and how does it resolve the problem? Perhaps you need to add explanation – Balwinder Singh Jul 26 '16 at 01:46
  • If the user is not authenticated (not logged in) then you don't have their username to add to the default? You can't do "dynamic" and "hardcode" at the same time. Also, in general, there's surely no sane "default username" value? – James Sep 07 '22 at 10:45