Maxim
Maxim

Reputation: 3946

Displaying cache headers

Symfony 2 is displaying cache headers, i was wondering how i could hide those

examples

HTTP/1.0 200 OK Cache-Control: no-cache Content-Type: text/html Date: Fri, 18 Jan 2013 19:07:08 GMT HTTP/1.0 200 OK Cache-Control: no-cache Date: Fri, 18 Jan 2013 19:07:08 GMT X-Debug-Token: 50f99d5cba4da

i am using this in my code

return new Response($this->renderView('Shout/view/default.html.twig'));

which gets called by

$httpKernel = $this->container->get('http_kernel');
        $response = new Response;
        $response->SetContent($httpKernel->forward('MyBundle:Module/'.$module.'/'.$module.':index'));
        $response->headers->set('Content-Type', 'text/html');
        return $response;

in a twig extension

Upvotes: 2

Views: 3886

Answers (3)

David Lefkon
David Lefkon

Reputation: 289

Add ->getContent() to end.

http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Response.html

    $response = $this->forward("MyBundle:Module:$module", array(
            'customer_id'  => $customer_id
    ))->getContent();

    return $response; 

Upvotes: 6

Maxim
Maxim

Reputation: 3946

For everyone that is looking for an answer, i continued on the above post and tried to use the render function from twig but with my custom module

this is was i got and it works fine :)

return $this->container->get('templating.helper.actions')->render('MyBundle:Module/'.$module.'/'.$module.':index', $attributes, $options);

Upvotes: 1

Dutow
Dutow

Reputation: 5668

Headers are part of the Response class inside HttpFoundation. You can manage them using the headers attribute, the same one you used in your code. That attribute is an instance of the ResponseHaderBag class, which has a remove function.

The header you want to remove is named 'Cache-Control', so if you write:

$response->headers->remove('Cache-Control');

It will remove that header. But if you check the source of Response, you will see that some of it's functionality depends on this header, so I'm not sure that removing this is really a good idea.

By default this header does nothing wrong, just returns 'no-cache', which means the browser won't cache your page. But you won't be able to cache a page without this header.

If your goal is to manually send the cache-control header yourself, consider using Symfony2's built in functionality instead.

Upvotes: 2

Related Questions