I need to set some HTTP headers "Expires", "Cache-Control",
"Last-Modified", for resources as CSS files, Images files, js files,
etc (Webroot content).
I've read that there's some functionality, through
Configure::write('Asset.timestamp', true); // In core.php
and the assetTimestamp method of the Helper class.
Now, the question is: How is it used?
I read the HtmlHelper code and in the css method, line 361 there's this:
$url = $this->assetTimestamp($this->webroot($path));
Solved.
First of all you have to consider to make it through Apache. You can take a look at this guide:
http://httpd.apache.org/docs/2.2/caching.html
The thing is that CakePHP has a method to do this. And is pretty good.
I'll explain this for CSS files. Of course can be used to JS content as well.
1) In your core.php file (under app/config/) uncomment this line:
Configure::write('Asset.filter.css', 'css.php');
That line says to CakePHP to route all requests to CSS files through that "css.php" script. As the name implies, it's a filter. There we can do whatever we want.
2) Create that "css.php" file. You've to create it under app/webroot/
Make there, you can take the file that the browsen is requesting and apply some cache HTTP headers.
Something like:
$filepath = CSS . $regs[1]; //There are some variables that are can be used in this script, take a look to de docs.
$output = file_get_contents($filepath);
header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT');
header("Content-Type: text/css");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + DAY) . " GMT"); //WEEK or MONTH are valid as well
header("Cache-Control: max-age=86400, must-revalidate"); // HTTP/1.1
header("Pragma: cache"); // HTTP/1.0
print $output;
That's it! There your content will be served with those headers specified and the browser will know that can cache them.
Take a look at:
http://www.bunchacode.com/programming/get-cakephp-build-in-css-compression-to-work/
There's a good version of css.php that also minfies it.