【问题标题】:Listener doctrine.event_listener:onFlush, security.token_storage always null侦听器学说.event_listener:onFlush, security.token_storage 始终为空
【发布时间】:2017-03-08 12:59:26
【问题描述】:

我正在尝试在我编写的 Listener 中获取当前登录的用户(在 symfony2 中工作,但不再在 Symfony 3.2.2 中工作)。

我的问题是 security.token_storage->getToken() 总是为空。

我发现的提示是此时可能不会执行 securityListener;所以我没有得到令牌 -> 因此没有用户。

这是服务声明(service.xml)

acme.history_logging_listener:
  class: Acme\EventListener\HistoryLoggingListener
    arguments:
      - "@service_container"
      - "@security.token_storage"
    tags:
      - { name: doctrine.event_listener, event: onFlush }

这是类代码

public function __construct(ContainerInterface $oContainer, TokenStorageInterface $oTokenStorage)
{
    $this->_oTokenStorage = $oTokenStorage;
}

public function onFlush(OnFlushEventArgs $eventArgs)
{
    \dump($this->_oTokenStorage->getToken()); // should contain a value (not null)
    die();
}

编辑

令牌将在第二次调用监听器后填充;第一次迭代没有数据;所以对我来说没问题。

【问题讨论】:

    标签: php doctrine-orm listener symfony


    【解决方案1】:

    能否将其设置为另一个事件中的私有变量,例如prePersist 和/或preUpdate,然后访问onFlush

    以这种方式在侦听器中设置的成员应在任何请求期间持续存在。

    【讨论】:

    • 感谢提示!我试了一下,检查了令牌,没有成功;但是在侦听器的第二次调用中(不知何故),令牌已被填充:) 我认为解决方案是不使用 die() 并检查以后的迭代:]。到目前为止,数据还不存在,因此令牌在第二次可用并满足我的需求。谢了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多