捕获会话超时Symfony2 [英] Catch session timeout Symfony2

查看:80
本文介绍了捕获会话超时Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Symfony2有疑问,希望有人能帮助我. Symfony在哪里检查用户会话,而做什么是没有会话.就像重定向到登录页面一样.

I have a question about Symfony2 and I hope someone can help me out. Where does Symfony checks the users session and what to do is there is no session. Like redirect to the login page.

我发现了类似的问题,但不是我真正的意思.

I found some similar question, but not really what I mean.

为什么我想知道?如果存在会话超时.我想检查呼叫是否为XmlHttpRequest.如果是这样,我想返回一个JSON,以便javascript可以处理它.如果不是,请按照正常方式进行操作.

Why do I want to know it? If there is a session timeout. I want to check if the call is a XmlHttpRequest. If so, I want to return a JSON so the javascript can handle it. If notn do it the normal way.

谢谢!

推荐答案

您必须创建侦听器

注册事件监听器和订阅者

config.yml:

config.yml :

services:
    mycompany.demobundle.listener.request:
        class: MyCompany\DemoBundle\RequestListener
        arguments: [@router, @security.context]
        tags:
             - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

并在RequestListener中测试会话是否超时:

and test in RequestListener if the session is timeout :

$inactive = 600; 
$session_life = time() - $request->getSession()->('timeout');
if($session_life > $inactive && $request->isXmlHttpRequest() )
    {  
         $headers['Content-Type'] = 'application/json';
         return new Response(json_encode($data), $status, $headers);

这篇关于捕获会话超时Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆