我正在尝试从侦听器事件“onKernelRequest”获取用户会话,但是security.context尚不存在并且每次都返回null。
如何创建在安全上下文后读取的请求侦听器?
谢谢。
class CurrencyListener implements EventSubscriberInterface {
private $default_currency;
private $serviceContainer;
public function __construct($default_currency, $serviceContainer) {
$this->default_currency = strtoupper($default_currency);
$this->serviceContainer = $serviceContainer;
}
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
var_dump($this->serviceContainer->get('security.context')->getToken());
}
public static function getSubscribedEvents() {
return array(
KernelEvents::REQUEST => array(array('onKernelRequest', 15)),
);
}
}
答案 0 :(得分:2)
将“onKernelRequest”侦听器优先级更改为小于8(防火墙的默认优先级)。