Catch session timeout Symfony2

拈花ヽ惹草 提交于 2019-12-07 04:54:26

问题


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.

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.

Thanks!


回答1:


you must create the listener

Registering Event Listeners and Subscribers

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 }

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);


来源:https://stackoverflow.com/questions/10846970/catch-session-timeout-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!