Skip to content

Instantly share code, notes, and snippets.

@rodnaph
Created January 21, 2015 10:06
Show Gist options
  • Save rodnaph/4edfaa67878d30894c1e to your computer and use it in GitHub Desktop.
Save rodnaph/4edfaa67878d30894c1e to your computer and use it in GitHub Desktop.
<?php
namespace MyApp\MyBundle\Listener;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* @DI\Service()
* @DI\Tag(name="kernel.event_listener", attributes={"event" = "kernel.request"})
*/
class SessionListener
{
/**
* @DI\Inject("router")
*/
public $router;
/**
* @DI\Inject("session")
*/
public $session;
/**
* {@inheritDoc}
*/
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
if ($routeName = $event->getRequest()->get('_route')) {
$route = $this
->router
->getRouteCollection()
->get($routeName);
if ($route) {
if (false === $route->getOption('session')) {
$this->session->save();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment