Snappad

14 Aug, 2008

Serving up Different Content Types with File Format Awareness

Posted by: Zach In: Tutorials

Life is great. You spent a couple hours building a great query to summarize data exactly how you want it, then you put it into a beautiful HTML view. But wait, your client wants to access this data with XML as well! Now what? Have no fear! File Format Awareness is here!

What is File Format Awareness?

Simply put, File Format Awareness allows you to add an extension to the end of the filename of your URL in order to change the view file that snappad loads while still loading the same controller. It also allows modifies the HTTP Content-Type to be the file format that was specified.

How can I use it?

To use File Format Awareness, simply add an extension to the end of the filename in your URL. This can be any extension you desire. Then, add a view file that corresponds, for example a page accessed via: http://www.projectname.com/feed.rss would have a view file with the name views/feed.rss.php.

The feed.rss would still use the controllers/feed.inc.php controller. This means you wouldn’t need to write a new controller to retrieve the same data, and you could easily add a views/feed.atom.php which would need no further controller modification. Hooray!

How does it work?

File Format Awareness is actually a rather simple thing. First it checks to see if there is a file extension being requested. Second, it assigns that extension to the $snappad->format variable as well as sets the $snappad->content_type. Third, it loads the controller as in the normal snappad program flow. Fourth it sends the $snappad->content_type as an HTTP Content type and finally loads the view file with the $snappad->format.php extension.

No Responses to "Serving up Different Content Types with File Format Awareness"

Comment Form

Categories

Download

Get started now by downloading the Snappad Framework!