CakePHP 1.2 RSS
So oddly RSS in CakePHP 1.2 came up a lot one night recently in the Cake IRC channel. There's a really good article on the Bakery about it, but that's for CakePHP 1.1. In 1.2 it's MUCH easier to do, there's less code. So here's a short how-to, though I'm sure it's published elsewhere, the more places one can find from Google the better.
First in your /app/config/routes.php file you need to setup the routing to handle the rss extension. So just add this line:
Router::parseExtensions('rss');
Then you'll need to make some changes and add a method in your controller.
// make sure you have text and time helper, the rest are whatever else you need
var $helpers = array('Html', 'Form', 'Javascript', 'Text', 'Time');
// example method i have here, but your find methods and model will be different of course
function latest() {
// shows latest posts
// So the first thing here checks to see if a .rss has been requested...
// if so get what we need for the feed
$isFeed = ife($this->RequestHandler->prefers('rss') == 'rss', true, false);
if ($isFeed) {
$this->pageTitle = 'Latest Posts Feed';
$this->set('channel', array('description' => 'Latest posts'));
}
$limit = 10;
$this->set('posts', $this->Post->findAll(null, null, 'date desc', $limit, null, -1));
}
Then we'll go over to our view template.
// this file is located under /views/posts/rss/latest.ctp
items($posts, 'transformRSS');
function transformRSS($data) {
return array(
'title' => $data['Post']['title'],
'link' => 'view/'.$data['Post']['lid'],
'guid' => 'view/'.$data['Post']['lid'],
'description' => $data['Post']['body'],
'author' => $data['Post']['author'],
'pubDate' => $data['Post']['date']
);
}
?>
Just take note that your model and fields will differ from what I've output in the feed. "lid" is something I created for the link id...which is part of a friendly url system I have setup for example. Everything should explain itself here, but the most important note is the location of the template file. It should not only be the name of the method (like usual) but should be located under the /rss/ directory under the model's view directory where the other templates are.
That's pretty much all there is to it. RSS is quite simple to setup compared to what you neede to do with CakePHP 1.1. The main difference is a lot of the setup and creation of the xml document is built into 1.2 for you.
You can see this specific example in action on my media blog latest post feed.
Other Blogs 



The code snippets are not shown here, neither in Firefox nor in Konqueror…
Comment by Daniel Hofstetter — February 7, 2008 @ 1:21 am
Yea, I noticed the path to the flash file wasn’t correct in the plugin I picked up — it would only work from the front page of my blog. So I just wrapped everything with pre tags. No pretty colors for now, bummer. Thanks for pointing that out.
Comment by Tom — February 7, 2008 @ 9:15 am