简体   繁体   English

symfony 3 Distrine LIstener服务注入token_storage不起作用

[英]symfony 3 Doctrine LIstener service inject token_storage doesnt work

I have a doctrine listener which needs the get the current logged in user. 我有一个学说侦听器,它需要获取当前登录的用户。

class DoctrineListener
{
    /**
     * @var null|TokenInterface
     */
    private $token;

    /**
     * DoctrineListener constructor.
     *
     * @param TokenStorageInterface $tokenStorage
     */
    public function __construct(TokenStorageInterface $tokenStorage)
    {
        $this->token = $tokenStorage->getToken();

        var_dump($this->token);
    }

and in my service.yml: 在我的service.yml中:

doctrine.listener:
    class: AppBundle\EventListener\DoctrineListener
    arguments:
      - '@security.token_storage'
    public: false
    tags:
      - { name: doctrine.event_listener, event: preFlush, method: preFlush }

The dump always returns me null when I try to use it in this listener. 当我尝试在此侦听器中使用转储时,转储总是返回null。 I inject the token_storage_service in other services and it works well. 我将token_storage_service注入其他服务中,并且效果很好。

I'm under symfony 3.1, with a rest API. 我使用的是symfony 3.1,带有其余的API。 And i send my authorizations header with Postman. 然后我将我的授权标头发送给Postman。

Can someone tell me what's wrong with my code ? 有人可以告诉我我的代码有什么问题吗?

Thanks in advance. 提前致谢。

尝试在preFlush方法中而不是在构造函数中调用$tokenStorage->getToken()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM