Disable Browser Cache in Cakephp

Published on : July 22, 2011

Author:

Category: Our Blog


When developing web apps, sometimes it is better to disable browser cache. We can do it in php by following code


header('Cache-Control: no-store, private, no-cache, must-revalidate');     // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false);  // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');                  // Date in the past
header('Expires: 0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');

So how do we set these in cakephp? Very simple


$this->disableCache();

This will produce headers like this


Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: [current datetime] GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache

I recommend calling this function in certain function rather than calling it beforeFilter() function in app_controller.php

see Sweet Simple Caching for view caching


Leave a Reply

Your email address will not be published. Required fields are marked *