Maybe there’s already a solution and I didn’t find it?! Right now im using header(‘Content-type:…’); all the time - but json is always the same and as I learned the last few years I shouldn’t repeat shouldn’t repeat shouldn’t repeat shouldn’t repeat myself.
Another approach of other frameworks is a simple convention via "suffix":
In this case you could differ in the controller wether it’s an ajax request, you’d choose the json variant of the view, if it’s a simple Get request, you’d choose the html variant.
I would recommend finding a framework that handles responses based on requests, such as CakePHP, and then just grab the code and implement as an extension (or just drop into the Vendors folder, whichever)
So why not add some simple methods in your base controller that can create that bunch of headers for you? Like sendMimeHeader($type)? If you need to render content from files, add some wrappers around these methods that also perform rendering, like renderMimePartial($type,$view,$params,…).
I’m not sure about the filename format you proposed. The ending .php will confuse lots of editor’s syntax highlighting.
In my opinion it’s elementary to controll the headers. So why not automatically send out an expires HTTP header if the controller filter is set to “cache the whole site for 1 day”? There’s no need for the browser to request this site within the next 24 hours again from your server - it’ll be be delievered right from the servers’ cache and it’s already in the browser’s cache. This would save a lot of requests!
In nearly every project there’s the time where a json, rss, excel, pdf, or any kind of media file runs through an PHP controller. You may just want to count the downloads, restrict the access to authorized groups or
slightly modify it when it’s downloaded (e.g. unique watermark or serial no.).
I’d rather abandon the zii ajax gimmicks than something elementary than headers.
To cut a long story short:
I’ve written an extension which parses a mime.types file (caches it) and provides some predefined “named scopes” like “nocache”, “public”, “expires”=>500. The name of the static function defines the mime type.
[*]Use it in your controller right before the output starts ($this->render(), $this->renderText(), or just readfile()).
A few examples:
// Setting a plain text header:
// Setting this png to private:
// Telling the browser, to cache it 1 hour
// Telling the browser not to cache
// Setting individual headers
mtHeader::png(array('GreetingsTo' => 'my mom'));
// Force download with given file name "setup.exe" 1kb size
mtHeader::exe(array('download' => array('setup.exe', 1024)));
Inlucded scopes are:
length [size in bytes]
download [file name and optional file size in bytes]
You also can combine scopes using an array as argument list. Even some combinations don’t make sense as e.g.
Feel free to add more scopes or improve this script. It’s be nice to publish this here too.
Ohh … nearly forgotten: This only works under php5.3 . You may also find a way to make it work in lower versions. As I’m using 5.3 and just wanted to share this little helper, I won’t develop it to be compatible with other versions
Could you write more detailed instruction for installing and using your class?
You wrote that the class is not classic Yii extension, so does it need to be registered in main.php config file?
Please be more clearer in your 3. instruction "Drop it also into your extensions folder". Is this mean to copy the mime.types file to extension folder?
include(mtHeader.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory
00329: * @param string class name
00330: * @return boolean whether the class has been loaded successfully
00332: public static function autoload($className)
00334: // use include so that the error PHP file may appear
00337: else if(isset(self::$_classes[$className]))
00342: return class_exists($className,false) || interface_exists($className,false);
00344: return true;
00348: * Writes a trace message.
00349: * This method will only log a message when the application is in debug mode.
00350: * @param string message to be logged
00351: * @param string category of the message
00352: * @see log
My remark above was incorrect. This is a case where I want the browser to do it’s default behavior–which is to CACHE. Adding the headers below, manually, to my main layout file achieves this in Firefox (but not MSIE):
I still haven’t figured out how to control the “Expires” header. See above. I want the browser to CACHE a given page, so that the user can use the Back button and still have form inputs populated. Ideas?
I downloaded mtHeader.php and it now sets Expires, thanks. So now the issue isn’t with mtHeader, which works perfectly, but rather how to OVERRIDE Yii’s default behavior of disabling browser caching.
I.e., the headers shown below cause Firefox to CACHE, but they fail to forse MSIE to cache when using the Yii framework.
This is what I included in my controller, just before calling render()
$expires = 60*60*24*14; // seconds * minutes * hours * days = 14 days
'Last-Modified'=>'Wed, 1 Dec 2010 08:08:08 GMT'
In Firebug, I see these headers:
Expires : Fri, 17 Dec 2010 01:49:00 GMT ## Correct. 14 days in the future
Cache-Control : max-age=1209600, public, s=maxage=1209600
Pragma : public
Last Modified : Wed, 1 Dec 2010 08:08:08 GMT ## Correct
Can anyone out there tell me how to force MSIE to cache?