Building a Custom RSS Feed Using Single Pages, Controllers, and the Page List class.

While the concrete5 page list block provides an RSS feed option, it isn't perfect. Its URL is a little unwieldy, and changes to the page list block may interfere with that RSS feed working. Additionally, I had some filtering requirements for my RSS feed that the page list couldn't quite provide. Those problems are on our radar, but I needed a fix now!

So what did I do? I made a custom single page and controller, at the /feed/ URL. Here are the components:



    class FeedController extends Controller {

       public function view() {
          header('Content-type: text/xml');
          echo "<?xml version=\"1.0\"?>\n"; ?>

       <rss version="2.0">
          <link><?=BASE_URL . DIR_REL?>/feed</link>
          <description>The written words and audio of Andrew Embler, 
          on topics such as concrete5, the web, personal computing, music and art.</description> 

          $pl = new PageList();
          $posts = $pl->get(10);
          $nav = Loader::helper('navigation');
          foreach($posts as $p) { ?>

               <link><?=BASE_URL . $nav->getLinkToCollection($p)?></link>
                <? $a = new Area('Main'); $a->display($p); ?>
                <a href="<?=BASE_URL . $nav->getLinkToCollection($p)?>">Comment on this at</a>
               <pubDate><?=date( 'D, d M Y H:i:s T', strtotime($p->getCollectionDatePublic())) ?></pubDate>
          <? } ?>





As you can see, pretty straightforward. Call the PageList class, filter by post type, setup sorting, and print out RSS XML.


This file is empty!

The controller contains the entire logic of the feed generator. The feed.php single page only exists so concrete5 will allow us to create it as a single page, through the dashboard. Simple and easy.

Loading Conversation