Catch session timeout Symfony2

firewall, php, symfony

Solution

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

Problem

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!

Original source