How to get the current logged User in a service

前端 未结 7 1119
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 20:25

In Symfony 2.8/3.0, with our fancy new security components, how do I get the currently logged User (i.e. FOSUser) object in a service without

7条回答
  •  渐次进展
    2020-12-03 20:59

    Works with Symfony 3.4, 4.x, 5.x & above. The Security utility class was introduced in Symfony 3.4.

    use Symfony\Component\Security\Core\Security;
    
    public function indexAction(Security $security)
    {
        $user = $security->getUser();
    }
    

    https://symfony.com/doc/3.4/security.html#always-check-if-the-user-is-logged-in

提交回复
热议问题