简体   繁体   English

Symfony2 - $ this-> getUser()vs $ this-> get('fos_user.user_manager');

[英]Symfony2 - $this->getUser() vs $this->get('fos_user.user_manager');

I'm using FOSUserBundle. 我正在使用FOSUserBundle。 What is the difference between these two? 这两者有什么区别?

$this->get('fos_user.user_manager');

...and... ...和...

$this->getUser();

I've found I've used both of the above at different times and everything works fine. 我发现我在不同的时间使用了上述两种方法,一切正常。

I'm guessing the first one is from FOS and the second one is the default one, but I'm guessing I should always use the same one. 我猜第一个是来自FOS,第二个是默认的,但我猜我应该总是使用同一个。

This is one piece of code I've used: 这是我用过的一段代码:

$user = $this->getUser();
if($user) {             
    $email = $user->getEmail();
} else {
    $email = "no email";
}

..and another... ..另一个......

$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserBy(array('memberID' => '123'));

...so should I have used the same method for both? ...所以我应该为两者使用相同的方法吗?

With $this->getUser() is only a shortcut to 使用$this->getUser()只是一个快捷方式

$this->get('security.context')->getToken()->getUser()

So this means you get the user object according to the current security token. 这意味着您可以根据当前安全令牌获取用户对象。 It's perfect and easy, when you want to retrieve the actual logged in user. 当您想要检索实际登录用户时,它非常完美。

But if you want to get other users, fos_user.user_manager is the choice, as it has methods to find users easy and hiding the implementation behind. 但是如果你想获得其他用户,可以选择fos_user.user_manager ,因为它有方法可以轻松查找用户并隐藏实现。 And it provides also methods for creating new users and updating them. 它还提供了创建新用户和更新用户的方法。 And also if you retrieve the current logged in user with $this->getUser() and made modifcations to them, you should use the fos user manager to update them. 此外,如果您使用$this->getUser()检索当前登录的用户并对其进行修改,则应使用fos用户管理器更新它们。 Take a look in the docs for more! 查看文档了解更多信息!

They return different objects. 他们返回不同的对象。 $this->get('fos_user.user_manager') returns a FOS\\UserBudle\\Doctrine\\UserManager object and $this->getUser() returns a FOS\\UserBundle\\Model\\User object. $this->get('fos_user.user_manager')返回一个FOS\\UserBudle\\Doctrine\\UserManager对象, $this->getUser()返回一个FOS\\UserBundle\\Model\\User对象。 The former handles users and the latter is a user. 前者处理用户,后者是用户。 So no, you are using it right. 所以不,你正在使用它。

Where the two differ is in saving a user or creating a new user. 两者的不同之处在于保存用户或创建新用户。 If using the FOSUserBundle, you should always use the $this->get('fos_user.user_manager') method. 如果使用FOSUserBundle,则应始终使用$this->get('fos_user.user_manager')方法。 This gives you access to the updateUser() function that works with the FOSUserBundle to make sure it updates all the user attributes that you don't need to explicitly declare in your User model, like date_created and roles . 这使您可以访问与FOSUserBundle一起使用的updateUser()函数,以确保它更新您不需要在User模型中显式声明的所有用户属性,例如date_createdroles

That function is different than using Doctrine to persist() and then flush() the model. 该函数与使用Doctrine persist()然后flush()模型不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony2 $ this-> getUser()在其他控制器上为空 - Symfony2 $this->getUser() is empty on other controllers 服务“ security.context_listener.0”依赖于不存在的服务“ fos_user.user_manager” - The service “security.context_listener.0” has a dependency on a non-existent service “fos_user.user_manager” Symfony $this->getUser() 没有 doctrine 关联 (OneToMany) - Symfony $this->getUser() without doctrine associations (OneToMany) ServiceNotFoundException:服务“ my.facebook.user”具有对不存在的服务“ fos_user.user_manager”的依赖 - ServiceNotFoundException: The service “my.facebook.user” has a dependency on a non-existent service “fos_user.user_manager” 对symfony2和fos用户进行多重检查 - Mutltiple check on symfony2 and fos User symfony2 fos用户表单标签 - symfony2 fos user form labels Symfony2,isValid()和$ this-> get('validator')之间的区别 - Symfony2, difference between isValid() and $this->get('validator') $ this-> render和$ this->重定向Symfony2之间的区别 - Difference between $this->render and $this->redirect Symfony2 Laravel表单请求-授权中的$ this-> user()与auth()-> user() - Laravel form request - $this->user() vs. auth()->user() in authorization 如果条件总是在返回用户“ $ this->'library_name'-> getUser()的同时返回零 - If condition is always returning zero while getting the user in " $this->'library_name'->getUser()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM