symfony-2.8

How to get the current logged User in a service

冷暖自知 提交于 2019-11-28 01:47:15
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 injecting the whole container? Is it even possible in a non-hacky way ? PS: Let's not consider the " pass it to the service function as a parameter " for being trivially obvious. Also, dirty. 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

How to get the current logged User in a service

旧街凉风 提交于 2019-11-26 22:00:17
问题 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 injecting the whole container? Is it even possible in a non-hacky way ? PS: Let's not consider the " pass it to the service function as a parameter " for being trivially obvious. Also, dirty. 回答1: Inject security.token_storage service into your service, and then use: $this->token_storage->getToken()->getUser(); as described here: http://symfony.com