Creating an RSS Feed for a Symfony Blog

Introduction

One of the features I've wanted to add to this blog for a while now, but kept putting off, until now, is an RSS feed. I recently created an account on connect.symfony.com and there was a spot to add an RSS feed to one's profile, which was the only thing stopping me from getting my profile to 100% and earning the "Profile completed" badge. Nothing like a little gamification to make me stop putting off this task.

Since I'm never one to re-invent the wheel, without justification, I of course looked to see if there were any Symfony bundles that would allow me to easily add an RSS feed to my blog. "Easily" being the keyword in that sentence. I did find several of bundles, but all of them seemed to have convoluted configuration, with a learning curve much steeper than rolling my own.

Oh, yea, you can see it in action here: https://eidson.info/rss

Rolling My Own

So, with a mission in hand I headed over to my google machine. The first handful of hits showed an implementation using PHP's XMLWriter, which is probably the 'right' way to do it, and would have been the better choice, if the XMl I needed to generate was any more complex than it was, but it seemed like overkill for such a simple string. Luckily, I stumbled across a post on talkerscode which gave me a good starting point.

Generating the XML

The first class that I created was a class to create my xml string that I wanted to return as my response (shown below). It is basically a simple static function that takes an array of posts and creates a properly formatted XMl string creating an item element for each post. When I tried to run this, the first couple of posts were handled properly, and then it choked and died. It turns out that the title of one of my post's contained an ampersand ('&'), which is not handled correctly by an xml parser, So I created a little helper function to escape any characters that were going to through the XML parsers for a loop.

Creating a Controller

So now that I had the XML generation working smoothly, I needed a way to handle an incoming request and return the appropriate response, which consists of the Content-Type: text/xml; header and the XML string. So I was able to create a Symfony controller that fetched the posts, created a response, set the headers, added the XML content, and returned the response.

Conclusion

Now I had my rss feed up and running, I was able to get my connect.symfony.com profile to 100%, earn a badge, and scratch "create an RSS feed" off of my to-do list. There are a couple improvements that could be made. When I get more than 15 or 20 posts, I'll probably limit the response to the last 10 or so posts, and if I ever start to get a lot of traffic, I'll probably cache the XML and only update it when a new post is added, but remember kids, "premature optimization is the root of all evil"!

Thanks for reading. Feedback and/or questions are always appreciated!

If you liked this post, you can subscribe to the rss feed or follow me @ToddEidson
on Twitter to be notified of future blog posts.