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- function rss() {
- Configure::write ('debug', 0);
- $posts = $this->Post->find('all');
- 'title' => "Brett's Blog Feed | Wilton Software Limited",
- 'description' => 'wiltonsoftware.com - Tips and notes on software design and development',
- 'link' => '/posts/rss',
- 'url' => '/posts/rss',
- 'language' => 'en',
- '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>"
- )
- );
- $this->RequestHandler->respondAs('rss');
- $this->viewPath .= '/rss';
- $this->layoutPath = 'rss';
- }
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
- function transform_rss($posts) {
- 'title' => $posts['Post']['title'],
- 'description' => $posts['Post']['body'],
- 'author' => "Brett Wilton",
- 'pubDate' => $posts['Post']['modified']
- );
- }
- ?>
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 TextThat 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 TextRelated
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.