How to get the current logged User in a service

前端 未结 7 1115
爱一瞬间的悲伤
爱一瞬间的悲伤 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 21:24

    Inject security.token_storage service into your service, and then use:

    $this->token_storage->getToken()->getUser();
    

    as described here: http://symfony.com/doc/current/book/security.html#retrieving-the-user-object and here: http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services

提交回复
热议问题