Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch session timeout Symfony2

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!

like image 812
jeroenjoosen Avatar asked Jun 01 '12 08:06

jeroenjoosen


1 Answers

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);
like image 125
a.aitboudad Avatar answered Oct 24 '22 00:10

a.aitboudad