【发布时间】: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