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…
Thank you… You helped me very.
Thank you. Again :razz:
Hi,
Can you please elaborate where should the above code be added? is it to the response.php?
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?
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.
Thanks Andy! That helps a bunch.