OctoberCMS RSS Feed

October is a content management system (CMS) based on the Laravel framework.  Many of my readers will already know that I am a huge fan of Laravel.  The framework makes development workflow a breeze and takes care of a lot of the mundane tasks. Linuxphile is, in fact, built on Laravel.  I had also developed http://twistedtastes.com using Laravel.  After the development of Twisted Tastes my wife and I came across October.   

There are a number of blogs out there on the usefulness of OctoberCMS so I'll skip over that here and focus on the RSS functionality I needed to implement for Holly's Twisted Tastes site. I am never one to reinvent the wheel and if a plugin or piece of functionality exists already I won't bother writing it myself. 

When I went in search of an RSS plugin I found one in the OctoberCMS marketplace for $5.  The plugin was specific to another plugin within the marketplace and didn't meet my needs without customization.  Looking around I couldn't find much else on the topic.  A few forum posts but nothing concrete.  So I set about writing my own. 

Some knowledge of October might be required to follow along.  As CMS October has the idea of pages, surprise!  These are files that make use of the TWIG templating engine and also October's own ability to insert code in to a page.  When you create a page in October you are able to specify the route or path.  With that in mind I wandered if I might just create a page with a path as /rss.xml, setting the content type to XML. 

I found that I could do that exactly!  I was pleasantly surprised.  To create this RSS feed I realized all I needed was a page written in XML rather than HTML and enumerable list of objects, in this case recipes. 


To start you'll need to create a page. You can give whatever title you like, however, you will likely want the path to be /rss.xml.  Next we go to the code side of the page to create a variable to contain the list of objects we'll want to iterate through. The simple three lines below are enough to get you started. Keep in mind the function must be named onStart. The model however, can be a model of whatever you want to publish in your RSS. In this case we're using the Recipe model I've created for Holly's recipe blog.

function onStart() { 
    $this['posts'] = \Lightbee\RecipeBlog\Models\Recipe::where('published', '=', '1')->orderby('created_at', 'desc')->get();

Next we need to code the page itself. Do not associate this page with any of your layouts.  You want it to be empty.  Following the RSS 2.0 syntax we iterate through each of the recipes posts.  As I mentioned you can use any model, but pay attention to the required elements for RSS and make sure you're meeting the minimum requirements.  We've tested this RSS feed with Chrome, Safari and the Vienna RSS reader. 

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
        <title>Twisted Tastes Recipes</title>
        <description>Food. Photos. Twisted.</description>
        {% for item in posts %} 
                <a href="/recipes/{{item.title_slug}}"><img src="{{item.featured_img|media}}"/></a></description>
        {% endfor %}

To view a working example check out http://twistedtastes.com/rss.xml


Post Comment