Overriding Response Http Header in Joomla

By default Joomla will set the page not to cache sending ‘cache-control: no-cache,no-store’ and ‘Pragma: no-cache’. If your using a non-Joomla caching solution you might need to modify this response header.

You can override the default response header by using the JResponse::allowCache() function.

This will prevent the no-cache header values that Joomla sets.


jimport('joomla.environment.response');
JResponse::allowCache(true);

Then you can set your custom response header values using setHeader(). You will now be able to control the caching behavior of your pages.


$dt = gmdate('D, d M Y H:i:s', time()).' GMT';
JResponse::setHeader('Date', $dt, true );
JResponse::setHeader('Last-Modified', $dt, true );
JResponse::setHeader('Cache-Control', 'max-age=300,must-revalidate', true );

You can place this code just about anywhere in the view, controller, model etc…

Leave a comment

7 Comments.

  1. Thank you… You helped me very.
    Thank you. Again :razz:

  2. Hi,
    Can you please elaborate where should the above code be added? is it to the response.php?

  3. Hello,
    I am interested in this as I have just set-up a CDN. On there forums it said that the response headers Cache-Control: no-cache and Pragma: no-cache, should be changed. Where should the above code be added? is it to the response.php?

  4. Can place it in the view.html.php view entry file in display() for any view like this:

    class UserViewUser extends JView
    {
      function display( $tpl = null)
      {
         jimport('joomla.environment.response');
         JResponse::allowCache(true);
         $dt = gmdate('D, d M Y H:i:s', time()).' GMT';
         JResponse::setHeader('Date', $dt, true );
         JResponse::setHeader('Last-Modified', $dt, true );
         JResponse::setHeader('Cache-Control', 'max-age=300,must-revalidate', true );
                
       }
    }
    

    If you want to apply this site wide then put it into a system plugin’s onAfterInitialise event.

  5. Thanks Andy! That helps a bunch.

  6. Have you ever thought about publishing an ebook or guest authoring
    on other blogs? I have a blog centered on the same topics you discuss and would love to have you share some stories/information. I know my audience
    would enjoy your work. If you are even remotely interested, feel free to shoot me an e mail.

  7. Link exchange is certainly not else except it can be just placing the other person’s webpage link on the
    page at suitable place and also other person will even do same
    in support of you.