New in Cake: URL Extensions
Article posted by Joel Moss on 03 Jan 2007   |  
Hi all and happy new year. Hope you all had a great Christmas. I am hoping to increase my activity on this blog in the next year and post more tips and articles as I discover them. Let me start of with the first of a number of articles introducing whats new and changed in CakePHP 1.2. I have found URL Extensions to be extremely valuable when serving differing types of content in my Cake apps. They provide an extremely easy way to serve separate templates/views depending on what you want to see. Usually when you call http://MyCakeApp/controller/action the actions view would be pulled from the /app/views/controller/action.ctp template file. (The .ctp extension is the new standard extension used for view templates in Cake.) Which is fine if all you ever want to do is serve the same type of content all day. But what happens if you would like to display that same view in XML or RSS? I know there are other ways of doing this, but the use of URL Extensions is by far the easiest and quickest. So all you would do is call http://MyCakeApp/controller/action.xml. And instead of /app/views/controller/action.ctp being rendered, it would look for your view template at /app/views/controller/xml/action.ctp. If you want to do something different when the XML view is called, you can use
$this->params['url']['ext']
The above will output the extension name. For example, if http://MyCakeApp/controller/action.xml were called, the above would equal "xml". Hope that helps.

This site contains the musings of Joel Moss, and is powered by Codaset pages; a simple, yet powerful way to host your static site. Just commit and push your site to your free Git repository at Codaset, and that's it!