cakephp logo

RSS made easy with CakePHP 1.2

The CakePHP team really have made RSS feeds a breeze, the following steps are how I got it up and running on my blog posts.

Step #1

Add the following function to your /app/controllers/posts_controller.php file. You will need to make sure the RssHelper helper and RequestHandler component are available to your controller.

Show Plain Text
PHP code
  1.   var $helpers = array('Html', 'Form', 'Rss');
  2.   var $components = array('RequestHandler');
  3.   function rss() {
  4.     Configure::write ('debug', 0);
  5.     $posts = $this->Post->find('all');
  6.     $this->set('channel', array(
  7.       'title' => "Brett's Blog Feed | Wilton Software Limited",
  8.       'description' => 'wiltonsoftware.com - Tips and notes on software design and development',
  9.       'link' => '/posts/rss',
  10.       'url' => '/posts/rss',
  11.       'language' => 'en',
  12.       'image' => "<url>http://wiltonsoftware.com/img/rss_banner.gif</url><title>wiltonsoftware.com</title><link>http://wiltonsoftware.com</link><width>228</width><height>32</height>"
  13.       )
  14.     );
  15.     $this->set(compact('posts'));
  16.     $this->RequestHandler->respondAs('rss');
  17.     $this->viewPath .= '/rss';
  18.     $this->layoutPath = 'rss';
  19.   }

Setting the channel variable allows you to customize some of the settings on your feed with the <image> section allowing you to add a logo, I stumbled upon this ability on Jonathan snooks site.

Step #2

Add the following to your /app/views/posts/rss/rss.ctp.

Show Plain Text
PHP code
  1. <?php
  2.   function transform_rss($posts) {
  3.     return array(
  4.       'title' => $posts['Post']['title'],
  5.       'link'  => array('action' => 'view', $posts['Post']['id']),
  6.       'guid'  => array('action' => 'view', $posts['Post']['id']),
  7.       'description' => $posts['Post']['body'],
  8.       'author' => "Brett Wilton",
  9.       'pubDate' => $posts['Post']['modified']
  10.     );
  11.   }
  12.   echo $rss->items($posts, 'transform_rss');   
  13. ?>

Step #3

You should now be able to see your RSS feed at /posts/rss page, if you have legacy requirements you can also add a route to your /app/config/routes.php file to a specific file, for example :-.

Show Plain Text
PHP code
  1. Router::connect('/posts.rss', array('controller' => 'posts', 'action' => 'rss'));

That should be it, your feed should be up and running now!

You can also add the link feed to pages with the $html->meta() function.

Show Plain Text
PHP code
  1. <?php echo $html->meta('Blog feed', '/posts/rss'); ?>

Related

Filed under: Cakephp  Tags: Rss

2 Responses to “RSS made easy with CakePHP 1.2”

Hi, Having a hell of a time getting an RSS feed out of wordpress. Every tutorial I've seen for getting cakephp to show an RSS feed is incomplete or incorrect (mismatched variables etc.). I recently read your tutorial http://wiltonsoftware.com/posts/view/rss-made-easy-with-cakephp-1-2.

If I disable the line $this->RequestHandler->respondAs('rss'); everything appears to be perfectly valid, except it has two blank lines before the feed which seems to be screwing everything up.

If I try to view the file I just get an open file dialog that is completely blank. I've spent hours trying different tutorials. I hope you can help.

I found the issue. I had blank lines at the end of the two models that I was using to retrieve my posts. Thanks for your help and a working cakephp tutorial.

Sorry, comments have been closed for this post.
(default) 11 queries took 7 ms
NrQueryErrorAffectedNum. rowsTook (ms)
1DESCRIBE `posts`17171
2DESCRIBE `comments`11111
3DESCRIBE `tags`221
4DESCRIBE `categories`221
5DESCRIBE `posts_tags`221
6DESCRIBE `categories_posts`221
7SELECT `Post`.`id`, `Post`.`url`, `Post`.`title`, `Post`.`icon`, `Post`.`metadesc`, `Post`.`metakeys`, `Post`.`categories`, `Post`.`tease`, `Post`.`body`, `Post`.`private_body`, `Post`.`created`, `Post`.`modified`, `Post`.`status`, `Post`.`allow_comments`, `Post`.`tags`, `Post`.`hitcount`, `Post`.`hitcount_rss` FROM `posts` AS `Post` WHERE `Post`.`url` = 'rss-made-easy-with-cakephp-1-2' LIMIT 1110
8SELECT `Comment`.`id`, `Comment`.`post_id`, `Comment`.`body`, `Comment`.`author`, `Comment`.`url`, `Comment`.`email`, `Comment`.`ip`, `Comment`.`status`, `Comment`.`junk_score`, `Comment`.`created`, `Comment`.`modified` FROM `comments` AS `Comment` WHERE `Comment`.`status` = 2 AND `Comment`.`post_id` = (14) 221
9SELECT `Tag`.`id`, `Tag`.`tag`, `PostsTag`.`post_id`, `PostsTag`.`tag_id` FROM `tags` AS `Tag` JOIN `posts_tags` AS `PostsTag` ON (`PostsTag`.`post_id` = 14 AND `PostsTag`.`tag_id` = `Tag`.`id`) 110
10SELECT `Category`.`id`, `Category`.`category`, `CategoriesPost`.`post_id`, `CategoriesPost`.`category_id` FROM `categories` AS `Category` JOIN `categories_posts` AS `CategoriesPost` ON (`CategoriesPost`.`post_id` = 14 AND `CategoriesPost`.`category_id` = `Category`.`id`) 110
11UPDATE `posts` AS `Post` SET `Post`.`hitcount` = Post.hitcount + 1 WHERE `Post`.`id` = 1410