<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class CsvHeaderSubscriber implements EventSubscriberInterface
{
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => [
['addDownloadHeader', -2],
],
];
}
public function addDownloadHeader(ResponseEvent $event)
{
if (substr($event->getResponse()->headers->get('Content-Type'), 0, 8) == 'text/csv') {
$event->getResponse()->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
}
}
}