views:

931

answers:

2

What's the best way to use CakePHP to control the Expires headers for image and CSS files?

My host unfortunately doesn't support mod_expires or mod_headers, so I can't simply set these values in an .htaccess file. Right now there's no cache control for my files and a lot of unnecessary requests are made.

What's the most elegant way to pipe requests for asset files through Cake and set the appropriate headers via header()?

A: 

I would suggest writing a helper which takes the filename and then returns a URL to a PHP script which fetches the file and adds the necessary header. You can see an example of a basic helper here: http://bakery.cakephp.org/articles/view/image-resize-helper

Michael Mior
The extra step with the Helper seems pretty unnecessary, thanks for the response anyway. :)
deceze
The reason for the helper is so that you don't have to manually rewrite all your URLs when outputting your assets. If you don't mind doing this, then you can certainly just write a PHP script that writes the headers and then include that script when referencing an asset using CakePHP.
Michael Mior
A: 

I guess the best, not even necessarily Cake-specific way, is to create a script in /webroot that adds the headers, then use .htaccess directives to pipe requests for ./css/* and ./img/* through that file. I was just hoping there might be some functionality like this built into Cake already. :)

deceze