How to cache static content (css, images, js files) in CakePHP2?

I need to set some HTTP headers "Expires", "Cache-Control", "Last-Modified" for resources in the form of CSS files, image files, js files, etc. (Webroot content).

I read that there are some functions 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:

$url = $this->assetTimestamp($this->webroot($path));
+5
source share
1 answer

solvable.

, , Apache. : http://httpd.apache.org/docs/2.2/caching.html

, CakePHP . .

CSS. , JS-.

1) core.php( /config/ ) :

Configure::write('Asset.filter.css', 'css.php');

CakePHP CSS "css.php" script. , . , .

2) css.php. app/webroot/

, , , HTTP- .

- :

$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;

! , , .

:

http://www.bunchacode.com/programming/get-cakephp-build-in-css-compression-to-work/

css.php, .

+11

All Articles