Post a Comment

I've had a few requests asking how I built my RSS feed since I'm using a custom built Code Igniter blogging system. Actually, it was pretty straight forward, but I thought I'd take a few moments to outline step by step how I did it.

Most of the inspiration (and a lot of blatant stealing) is taken from this post about RSS on the Code Igniter forums. Please note, that this requires that you load the URL helper... which I autoload, but could just as easily be included in the contoller (thanks Attos). I start out by creating a controller named "feed".

An important thing to notice about the feed controller is that it sends a content-type along with it that indicates its a news feed - "application/rss+xml". Without that, browsers will try to display the file raw, or parse it as html. Both of those is not what we want. You'll also notice that it references a a model function called "getRecentPosts()". Here's that function inside the posts_model.

You'll notice reference to 'post_visible' in there. I just use that as a flag for whether or not I want a particular post "live". This gives me the ability to save and create drafts, without them being visible to the world. The controller also loads a view called 'rss'. Here's the view.

The view uses PHP to echo out the opening <?xml version="1.0"?> element. That's because I was having trouble with the server interpretting the opening <? as PHP. It's a pretty common problem actually. Finally, this line is noteworthy.

I reference images and other resources absolutely in the code, but for the feed that needs to be changed to a http:// path reference, so I'm just using using the PHP str_replace() function to rewrite the paths of the images in the feed.