Generating RSS feed
On this article we dicuss how to generate a rss feed output for the blog posts. We are going to make a simple output with the most basic information for the feed. This is the view file (core/views/rss.php)
<?php
header('Content-Type: application/rss+xml; charset=utf-8');
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><?=$title?></title>
<link><?=$link?></link>
<description><?=$description?></description>
<atom:link href="<?=Config::base()?>rss" rel="self" type="application/rss+xml" />
<?php foreach($items as $item) { ?>
<item>
<title><?=$item->title?></title>
<link><?=Config::base().'blog/'.$item->id?></link>
<guid><?=Config::base().'blog/'.$item->slug?></guid>
<pubDate><?=date('r',strtotime($item->updated))?></pubDate>
<description><![CDATA[<?=$item->post?>]]></description>
</item>
<?php } ?>
</channel>
</rss>
- The date('r') is used to output the date in this format: Sun, 23 Jul 2017 06:14:51 GMT
- permite us print html tags in description
Since its is a blog feed the controller to be used will be the blog class. We create the action feed:
function feedAction()
{
$title=Config::get('title');
$link=Config::base();
$description=Config::get('slogan');
$items=Blog::latestposts();
include 'src/core/views/rss.php';
}
The information is taken simply from the configuration variables. The method latestposts() returns a prefixed number of the latest published posts, in the form of generator of objects.
That's all it is, the path /blog/feed will return the rss feed.
The feed of this website (more attributes have been added in the feed):