fkoessler
fkoessler

Reputation: 7257

Symfony2: programmatically start session

I want a global listener in my app to redirect the user after the session has timed out.

I am listening to the kernel.exception event for the SessionUnavailableException:

public function onKernelException(GetResponseForExceptionEvent $event)
{
    $exception =  $event->getException();
    if ($exception instanceof SessionUnavailableException) {
        //start new session and set flash here...
        $response = new RedirectResponse($this->router->generate('homepage'));
        $event->setResponse($response);
    }
}

This code is executed after the session has timed out. The problem is if I leave the code like this, the exception will fire in an endless cycle. I need to start and save the session here, but can't find how to do this...

Any help?

By the way, is this a clever way to achieve my goal?

Upvotes: 2

Views: 570

Answers (1)

Alejandro Steinmetz
Alejandro Steinmetz

Reputation: 231

I think you can start the session adding the following code:

use Symfony\Component\HttpFoundation\Session\Session;

and...

$session = new Session();
$session->start();

..then you can set the flash messages:

$session->getFlashBag()->add(
                    'warning',
                    'El tiempo de su sesión se ha agotado. Por favor, vuelva a ingresar al sistema.'
);

Upvotes: 1

Related Questions