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 displa开发者_运维问答y the username in a layout.php, not in a particular module/template
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.
Try the following. One of them may work.
$this->getSfGuardUser()
$this->getGuardUser()
$this->getUser()->getSfGuardUser()
$this->getUser()->getGuardUser()
精彩评论