Creating valid RSS feed in Laravel applications.

Today somebody asked me on Twitter if my Laravel application, http://bootsnipp.com can have RSS feed to keep users updated on the new snippets that are posted. At first I thought it would take me a while to implement, maybe like a weekend project, but then I remembered that Laravel has a great community and lots of bundles that might offer the features I need to build the RSS feed.

Turns out it is easier than I thought.
About two hours later, I got a working and somewhat tested RSS feed that outputs RSS 2.0, Atom and RSS 0.92 feeds.
Check it out here :
http://bootsnipp.com/feed
http://bootsnipp.com/feed/atom
http://bootsnipp.com/feed/rss092

And here is a screenshot :
bootfeed.png

What did I do ? Simple!
1) Get the Feeder bundle by Proger_XP :
http://bundles.laravel.com/bundle/feeder

2) Install it :

php artisan bundle:install feeder

3) Add it to bundles.php:

[sourcecode language=”php”]’feeder’ => array(
// when the bundle is started all Feeder classes are automatically loaded
// so you can either autostart it or have autoloader mappings (more efficient).
//’auto’ => true,

‘autoloads’ => array(
‘map’ => array(
‘Feed’ => ‘(:bundle)/chained.php’,

‘FeedChannel’ => ‘(:bundle)/feeder.php’,
‘FeedEntry’ => ‘(:bundle)/feeder.php’,
‘Feeder’ => ‘(:bundle)/feeder.php’,
‘TextFeeder’ => ‘(:bundle)/feeder.php’,
‘FeedOut’ => ‘(:bundle)/feeder.php’,
),
),
)[/sourcecode]

4) Create a “feed.php” controller:

[sourcecode language=”php”]
<?php

class Feed_Controller extends Base_Controller
{
public function action_index()
{
$feed = Feed::make();

$feed->logo(asset(‘img/logo.jpg’))
->icon(URL::home().’favicon.ico’)
->webmaster(‘Your Name’)
->author&nbsp;&nbsp; (‘Your Name’)
->rating(‘SFW’)
->pubdate(time())
->ttl(60)
->title(‘My App Feed’)
->description(‘Newest stuff on MyApp.com’)
->copyright(‘(c) ‘.date(‘Y’).’ MyApp.com’)
->permalink(URL::home().’/feed’)
->category(‘PHP’)
->language(‘en_EN’)
->baseurl(URL::home());

// get latest 20 posts
$posts = Post::order_by(‘created_at’,’desc’)->take(20)->get();

foreach ($posts as $post) {
$feed->entry()->published($post->created_at)
->content()->add(‘text’, $post->text)->up()
->content()->add(‘html’, HTML::decode($post->text).'<br><a href="’.action(‘posts@view’, array($post->slug)).’"><img src="’.asset(‘uploads/’.(($post->attachment_id) ? (Upload::find($post->attachment_id)->small_filename) : "empty.jpg" )).’" /></a>’)->up()
->title()->add(‘text’,$post->title)->up()
->permalink(action(‘posts@view’, array($post->slug)))
->author()->name(‘By ‘.$post->author)->up()
->updated($post->updated_at);
}

$feed->Rss20();
// this is a shortcut for calling $feed->feed()->send(…);
// you can also just $feed->Rss20(), Rss092() or Atom();
}

public function action_atom()
{
$feed = Feed::make();

$feed->logo(asset(‘img/logo.jpg’))
->icon(URL::home().’favicon.ico’)
->webmaster(‘Your Name’)
->author&nbsp;&nbsp; (‘Your Name’)
->rating(‘SFW’)
->pubdate(time())
->ttl(60)
->title(‘My App Feed’)
->description(‘Newest stuff on MyApp.com’)
->copyright(‘(c) ‘.date(‘Y’).’ MyApp.com’)
->permalink(URL::home().’/feed’)
->category(‘PHP’)
->language(‘en_EN’)
->baseurl(URL::home());

$posts = Post::order_by(‘created_at’,’desc’)->take(20)->get();

foreach ($posts as $post) {
$feed->entry()->title()->add(‘text’,$post->title)->up()
->updated($post->updated_at)
->permalink(action(‘posts@view’, array($post->slug)))
->author()->name(‘By ‘.$post->author)->up()
->content()->add(‘html’, HTML::decode($post->text).'<br><a href="’.action(‘posts@view’, array($post->slug)).’"><img src="’.asset(‘uploads/’.(($post->attachment_id) ? (Upload::find($post->attachment_id)->small_filename) : "empty.jpg" )).’" /></a>’);
}

$feed->Atom();
}

public function action_rss092()
{
$feed = Feed::make();

$feed->logo(asset(‘img/logo.jpg’))
->icon(URL::home().’favicon.ico’)
->webmaster(‘Your Name’)
->author&nbsp;&nbsp; (‘Your Name’)
->rating(‘SFW’)
->pubdate(time())
->ttl(60)
->title(‘My App Feed’)
->description(‘Newest stuff on MyApp.com’)
->copyright(‘(c) ‘.date(‘Y’).’ MyApp.com’)
->permalink(URL::home().’/feed’)
->category(‘PHP’)
->language(‘en_EN’)
->baseurl(URL::home());

$posts = Post::order_by(‘created_at’,’desc’)->take(20)->get();

foreach ($posts as $post) {
$feed->entry()->published($post->created_at)
->content()->add(‘text’, $post->text)->up()
->content()->add(‘html’, HTML::decode($post->text).'<br><a href="’.action(‘posts@view’, array($post->slug)).’"><img src="’.asset(‘uploads/’.(($post->attachment_id) ? (Upload::find($post->attachment_id)->small_filename) : "empty.jpg" )).’" /></a>’)->up()
->title()->add(‘text’,$post->title)->up()
->permalink(action(‘posts@view’, array($post->slug)))
->author()->name(‘By ‘.$post->author)->up()
->updated($post->updated_at);
}

$feed->Rss092();
// this is a shortcut for calling $feed->feed()->send(…);
// you can also just $feed->Rss20(), Rss092() or Atom();
}
[/sourcecode]

6) Register “feed” controller within the routes.php:

[sourcecode language=”php”]Route::controller(‘feed’);[/sourcecode]

7) Done !  big_smile
Now if you go to your application /feed route, you should see the feed! Of course you need to replace the data with your model and your conventions.

Check out some docs by the bundle’s author at http://proger.i-forge.net/PHP_Feeder/7sg

Let me know if you have any questions!

Liked it? Take a second to support Maks Surguy on Patreon!
Become a patron at Patreon!

You may also like

8 comments

  • Proger_XP October 23, 2012  

    Hey, I’m totally glad to receive feedback on this one. Thanks for sharing it, Max!

  • John Kevin M. Basco October 25, 2012  

    Thank you for this one, very helpful 🙂 Anyway I saw your break dance vids in youtube! You got some damn awesome skills!

  • Kimberley Myers November 21, 2012  

    I have read your blog and I try your steps which you provide here in my application and proved very helpful.it grow up my knowledge.

  • noa January 15, 2013  

    Tried it and it works fine on mozilla and internet explorer but google chrome doesn’t display the result in the right format. It displays it with content type: text/html.

Leave a comment