在 Symfony 2.8/3.0 中,使用我们精美的新安全组件,我如何获取当前记录的User
(即 FOSUser) 服务中的对象without注入整个容器?
是否有可能在非黑客方式?
PS:我们不要考虑“将其作为参数传递给服务函数“因为这是显而易见的。而且,肮脏。
Inject security.token_storage
service 到您的服务中,然后使用:
$this->token_storage->getToken()->getUser();
如此处所述:http://symfony.com/doc/current/book/security.html#retrieve-the-user-object http://symfony.com/doc/current/book/security.html#retrieving-the-user-object和这里:http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)