<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class RequestListener
{
private SessionInterface $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!$event->isMasterRequest()) {
// don't do anything if it's not the master request
return;
}
$currentRoute = $request->attributes->get('_route');
// Skip if this is not a route or it's a route that you don't want to track
if (null === $currentRoute || $request->isXmlHttpRequest()) {
return;
}
// Get the last route from the session
$lastRoute = $this->session->get('last_route', null);
// Update the session with the current route
$this->session->set('last_route', $currentRoute);
// Optionally store more information, like parameters
$this->session->set('last_route_params', $request->attributes->get('_route_params'));
}
}