Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 279
Maxim Avatar asked Nov 20 '25 01:11

Maxim


2 Answers

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; 
like image 111
David Lefkon Avatar answered Nov 21 '25 14:11

David Lefkon


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.

like image 36
Dutow Avatar answered Nov 21 '25 13:11

Dutow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!