Oct 012008

RSS in cakephp 1.2 couldn’t be simpler, there are many options however the simplest way to do it is using the inbuilt RSS request handler.  This simply tells Cake to route requests with a specific extension (in this case .rss) to a different view, in which you can return native RSS using the inbuilt RSS helper.

The example below adds RSS support for a single action, with a single simple query.  Other examples can be found here RSS in CakePHP 1.2.

Lets assume you have a controller of “blog” and a view of “entries” i.e. www.mysite.com/blog/entries

To add RSS feed to this action, first add the ‘RequestHandler’ component in the controller’s (blog) $components attribute.

var $components = array('RequestHandler');

then ensure that the routing (i.e. the thing that listens for a specific extension) is set up by adding the following to  your app/config/routes.php


With these settings, you can tell cake to show RSS simply by adding the .rss extension to your standard url, that is:




Now to make it all work you now need to set up your RSS view.  Cake will expect this to be under an RSS folder in your appropriate view folder, in this case it would be /app/views/blogs/rss – the name of the file should be the same as that of the standard view you’re replacing, i.e. entries.ctp.

So, create your view /app/views/blogs/rss/entries.ctp

Inside you can put anything you want (you can manually create your RSS feed if you would like), but the simplest way is to use the RSS helper to format a standard cake recordset.  Assuming you have used a standard findall() query or have produced an array with similar data you can put the following in your new viewFile.

echo $rss->items($blogs, 'transformRSS');

//Callback function
function transformRSS($baserecord){
	return array(
		'title' => $blog['Blog']['title'],
		'link' => array('action' => 'view', $blog['Blog']['id']),
		'guid' => array('action' => 'view', $blog['Blog']['id']),
		'description' => $blog['Blog']['body'],
		'author' => 'blog@yoursite.com (MyBlog)',
		'pubDate' => $blog['Blog']['modified']

Note, the $rss-items($data,$function) loads the RSS feed, but the callback function turns it into data that can be used, obviously any and all of the information can be dynamic or fixed.

One problem I found when first loading this was a general formatting error ( IE indicated invalid characters line 3 and Firefox simply didn’t load the RSS.  I first validated it using the W3C feed validation service which revealed a typo, but not the source of the problem), on closer inspection it appeared I had white space at the top of the output generated.  This was caused by a stray trailing space on one of my controllers.  I identified and fixed this using this excellent whitespace analysis tool available from cakephptrac – using the command line tool.

Good luck

  3 Responses to “Simple RSS feeds in CakePHP”

  1. Thanks for sharing this. I was having the same problem with whitespace stopping my rss bookmarks from working. The whitespace tool at cakephptrac is pretty sweet.

  2. thankyou thankyou thankyou! exactly what i needed!

  3. Hi,

    Thanks for your post. it really helped me a lot. I want to display images in my Rss feed using cakephp. Can you tell me the parameters to be used for displaying the image.

    Thanks in Advance

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




This site uses Akismet to reduce spam. Learn how your comment data is processed.