Symfony Suppress Header Output

Zooey Deschanel

Zooey Deschanel


Here’s how to suppress the header out in a Symfony controller.

I was busting out a simple controller to handle the csv download of a log file and I kept getting this in the output.

HTTP/1.0 200 OK 
Cache-Control: no-cache 
Date: Mon, 01 Feb 2016 21:39:07 GMT

The problem was that I was calling render to load my Twig template.

$response = new Response();
$log = unserialize(file_get_contents(__DIR__ . '/../Resources/logs/example.log'));
$response->setContent($this->render(
            'MyBundle:Order:log.csv.twig',
            array(
                'log' => $log,
            )));
$response->headers->set('Content-type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename=example.log.csv');
return $response;

To fix this, all I needed to was call renderView instead.

$response = new Response();
$log = unserialize(file_get_contents(__DIR__ . '/../Resources/logs/example.log'));
$response->setContent($this->renderView(
            'MyBundle:Order:log.csv.twig',
            array(
                'log' => $log,
            )));
$response->headers->set('Content-type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename=example.log.csv');
return $response;
Share Button

Leave a Reply

Your email address will not be published.