20.11 | Add an RSS-Feed to your blog with Lithium

A common feature of every blog is a RSS-Feed. In this post I'll show you how to do this easily with Lithium and its View-Layer.

Adding an RSS-Feed to your blog with li3 is really straightforward, because all the infrastructure you need is already at your fingertips. I assume that you already have some posts stored in your database, you serve them to your users via /posts and the new RSS-File should be accessible via /posts.rss.

First, we need to tell Lithium that we want to add another Media-Type to our application. Add the following to your app/config/bootstrap/media.php-File (and don't forget to uncomment it in app/config/bootstrap.php). You can also comment the other code out in this file if you don't need it.

  1. use lithium\net\http\Media;
  2. Media::type('rss', 'application/rss+xml');

Next, let's add a link to our default.html.php-Layout so that our feed shows up in the browser.

  1. <?= $this->html->link('RSS-Feed', '/posts.rss', array('type' => 'rss')); ?>

Before we start to create our RSS-Views, check that your PostsController::index()-Method looks something like this and returns one or more posts to the View.

  1. public function index() {
  2. $posts = Post::all(array('order' => array('created' => 'DESC')));
  3. return compact('posts');
  4. }

Alright, first create a new default.rss.php-Layout (in app/views/layouts):

  1. <?php echo '<?xml version="1.0" encoding="UTF-8" ?>'; ?>
  2. <rss version="2.0">
  3. <channel>
  4. <title>My cool Blog</title>
  5. <description>A short description about my blog.</description>
  6. <link>http://exampleblog.com/</link>
  7. <lastBuildDate><?= date('D, d M Y g:i:s O'); ?></lastBuildDate>
  8. <pubDate><?= date('D, d M Y g:i:s O'); ?></pubDate>
  9. <?= $this->content(); ?>
  10. </channel>
  11. </rss>

The previous code is just basic RSS-Markup. Feel free to add a custom title, description, link and so on. The lastBuildDate and pubDate are generated dynamically. content(); ?> will render our posts-View which we'll add now. Insert the following code in app/views/posts/index.rss.php:

  1. <?php foreach($posts As $post): ?>
  2. <item>
  3. <title><?= $post->title; ?></title>
  4. <description><?= $post->teaser; ?></description>
  5. <link><?= $this->html->link($post->title, array('Posts::show', 'slug' => $post->slug)); ?></link>
  6. <guid isPermaLink="true">http://exampleblog.com/<?= $post->slug ?></guid>
  7. <pubDate><?= date('D, d M Y g:i:s O', $post->created->sec); ?></pubDate>
  8. </item>
  9. <?php endforeach; ?>

You may need to modify the attributes according to your database schema. If you need help on this, feel free to contact me or ask in #li3 on FreeNode.

That's it! You've added an RSS-Feed to your blog. You can now click the RSS-Icon in your browser or render it directly with http://exampleblog.com/posts.rss. BTW: that's exactly how I did it for this blog, so I hope that it should work for you too!