防止更新特定控制器的 MetadataBag 中的 LastUsed

RYN*_*RYN 2 session symfony

我编写了一个事件侦听器kernel.request,使我能够在用户闲置超过一段时间时自动注销用户。
我用它来计算空闲时间:

$idle = time() - $this->session->getMetadataBag()->getLastUsed()
Run Code Online (Sandbox Code Playgroud)

但是我的页面中有一个定期的 Ajax 请求(用于页面中的通知计数),并且它们不断更改LastUsed字段,MetadataBag因此空闲限制永远不会达到。
是否可以阻止特定Controller(即 ajax 控制器)更新会话LastUsed
如果是,如何?
如果没有,我还能做些什么来处理这个问题?

谢谢

use*_*997 5

我不知道如何阻止MetadataBag's的更新lastUsed,但是您可以在会话中手动设置用户上次请求的时间并使用它。
您可以创建如下所示的侦听器并使其侦听kernel.request事件,并在您的其他侦听器中,在会话中使用此侦听器而不是$this->session->getMetadataBag()->getLastUsed().

public function listen(GetResponseEvent $event){
  // in your listeners main function
  $request = $event->getRequest();
  $route = $request->attributes->get('_route');
  if($route != 'your_ajax_check_route'){
    // update the session and etc.
    // retrieve what you store here in your other listener.
  }
}
Run Code Online (Sandbox Code Playgroud)