src/EventSubscriber/CsvHeaderSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class CsvHeaderSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @inheritDoc
  10.      */
  11.     public static function getSubscribedEvents()
  12.     {
  13.         return [
  14.             KernelEvents::RESPONSE => [
  15.                 ['addDownloadHeader', -2],
  16.             ],
  17.         ];
  18.     }
  19.     public function addDownloadHeader(ResponseEvent $event)
  20.     {
  21.         if (substr($event->getResponse()->headers->get('Content-Type'), 08) == 'text/csv') {
  22.             $event->getResponse()->headers->set('Content-Disposition''attachment; filename="export.csv"');
  23.         }
  24.     }
  25. }