【问题标题】:symfony 3 Doctrine LIstener service inject token_storage doesnt worksymfony 3 Doctrine LIstener 服务注入 token_storage 不起作用
【发布时间】:2017-01-13 23:54:22
【问题描述】:

我有一个原则监听器,它需要获取当前登录的用户。

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);
    }

在我的 service.yml 中:

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

当我尝试在此侦听器中使用它时,转储总是返回 null。 我将 token_storage_service 注入到其他服务中,效果很好。

我在 symfony 3.1 下,带有一个 rest API。 我用 Postman 发送我的授权标头。

谁能告诉我我的代码有什么问题?

提前致谢。

【问题讨论】:

标签: php doctrine listener symfony


【解决方案1】:

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

【讨论】:

  • 就是这样。感谢您和@Frederico。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2016-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
相关资源
最近更新 更多