Symfony2:使用FOSUserBundle时如何在控制器内部获取用户Object? [英] Symfony2: How to get user Object inside controller when using FOSUserBundle?
问题描述
我正在使用FOSUserBundle来验证我的用户.
I'm using FOSUserBundle to authenticate my users.
我试图在Controller中获取用户对象以注册行程,在保存之前应在该行程中添加用户对象.
I'm trying to get the user object inside the Controller to register a trip where I should add the user object to this Trip before save.
我没有找到方法,因为我在symfony doc中找到了下一个方法:
I did not found how to do that because next method where I found it in symfony doc:
$user = $this->container->get('security.context')->getToken()->getUser();
将用户名提供为字符串,但是我需要整个对象.
renders the username as string, but I need the whole object.
当前,我使用此方法,但无法正常工作.
Currently, I use this method, but it's not working properly.
$username = $this->container->get('security.context')->getToken()->getUser();
$em = $this->container->get('doctrine')->getEntityManager();
$user = $em->getRepository('SiteUtilisateurBundle:Utilisateur')->find($username);
如何正确执行此操作?
推荐答案
我认为 Ramon 是正确的.您已经有用户对象.
I think Ramon is right. You already have the user object.
也可以在Symfony> 2.1.x中使用
Also in Symfony > 2.1.x you can use
$this->getUser();
在控制器内部.
这篇关于Symfony2:使用FOSUserBundle时如何在控制器内部获取用户Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!