I tried so far,
Path CoreBundle / DependencyInjection / configuration.php
namespace Funstaff\CoreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('funstaff_core');
$rootNode
->children()
->scalarNode('timeout')->defaultValue(900)
->isRequired()->end()
->end();
return $treeBuilder;
}
}
we will add this code in the file FunstaffCoreExtension.php
namespace Funstaff\CoreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class FunstaffCoreExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container,
new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->setParameter('core.timeout', $config['timeout']);
}
}
In config.yml.
funstaff_core:
timeout: 600
I created my RequestListener.php file in the following path: FunstaffCoreBundle / Request / Listener.
namespace Funstaff\CoreBundle\Request\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class RequestListener implements EventSubscriberInterface
{
protected $session;
protected $securityContext;
protected $timeout;
/**
* Construct
*
* @param Session $session
*/
public function __construct(Session $session,
SecurityContext $securityContext,
$timeout)
{
$this->session = $session;
$this->securityContext = $securityContext;
$this->timeout = $timeout;
}
/**
* Get Subscribed Events
*
* @return array event list
*/
public static function getSubscribedEvents()
{
return array(
'kernel.request' => 'onKernelRequest',
);
}
/**
* On Kernel Request
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$meta = $this->session->getMetadataBag();
$lastused = $meta->getLastUsed();
if (null !== $lastused && (time() - $lastused) > $this->timeout) {
$this->securityContext->setToken(null);
$this->session->invalidate();
}
}
}
& in Resources / config / services.yml
services:
timeout.request.listener:
class: Funstaff\CoreBundle\Request\Listener\RequestListener
arguments: [ @session, @security.context, %funstaff.timeout% ]
tags:
- { name: kernel.event_subscriber }
But its not working session timeout. Anything left me? Please Anyone have idea then let me know.