【发布时间】: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 发送我的授权标头。
谁能告诉我我的代码有什么问题?
提前致谢。
【问题讨论】:
-
它返回 null 因为安全侦听器尚未(尚未)执行
-
谢谢@Al Fonce,这对我也有帮助:)
标签: php doctrine listener symfony