Search code examples
phpsymfony1sfdoctrineguard

Display current logged-in user


I'm using Symfony and sfDoctrineGuardPlugin.

How do I display, in a template, the username of the current user ?

I've tried $this->getUser()->getUsername(), but all I get is the error : "Call to undefined method sfPHPView::getUser."

I'm trying to display the username in a layout.php, not in a particular module/template


Solution

  • In a template (view), use $sf_user:

    For the username:

    echo $sf_user->getUsername();
    

    For the id:

    echo $sf_user->getAttribute('user_id', null, 'sfGuardSecurityUser');
    

    Or, in an action (controller), you can do what you're doing in your question:

    $this->username = $this->getUser()->getUsername();
    

    and then $username will be available in your view template.

    See template shortcuts in the Symfony docs for other view variable shortcuts.