src/EventListener/NoCacheListener.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. class NoCacheListener
  5. {
  6.     public function onKernelRequest(RequestEvent $event): void
  7.     {
  8.         $route $event->getRequest()->attributes->get('_route');
  9.         // Add here your routes that should not be cached
  10.         $routeNames = [
  11.             'customer_show_cart',
  12.         ];
  13.         if (!in_array($route$routeNames)) {
  14.             return;
  15.         }
  16.         $response $event->getResponse();
  17.         if(!$response){
  18.             return;
  19.         }
  20.         $response->setMaxAge(0);
  21.         $response->headers->addCacheControlDirective('must-revalidate');
  22.         $response->headers->addCacheControlDirective('no-store');
  23.         $response->headers->addCacheControlDirective('no-cache');
  24.     }
  25. }