How to catch an id from sfUser?

六月ゝ 毕业季﹏ 提交于 2020-01-11 10:48:07

问题


I'm working with symfony 1.4 & i use sfDoctrineGuardPlugin as a tool for authentifications.

I want to catch the Id of user in order te store it in my table.

After a small research, I guess I have to use that expression : getUser()->getGuardUser()->getId().

What i looking for is how to invest that expression for my target.


回答1:


if the user is authenticated, yes, you can use:

// inside an action
$id = $this->getUser()->getGuardUser()->getId();

// inside a template
$id = $sf_user->getGuardUser()->getId();

If the user is not authenticated, it'll throw an error.




回答2:


Depends from where you are calling.

From the 'view':

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

From the 'controller' (as mentioned by Tom above):

echo $this->getUser()->getGuardUser()->getId();
echo $this->getUser()->getGuardUser()->getUsername();

(First line is 'id', second is 'username')



来源:https://stackoverflow.com/questions/6028718/how-to-catch-an-id-from-sfuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!