Feed and sitemap generation Part I

Posted by Damodar Bashyal on March 14, 2009

 

I am so excited to learn codeigniter more and more everyday through the means of my own new cms 'CodeFight CMS'. As i keep learning and implement new things in my CMS, i will keep posting here.

First addition after asset manager is FEED. After searching through codeigniter forum i found Derek Allard's post Building an RSS Feed in Code Igniter. Google thought the page's importance is not that high and ranked it low by giving 2 out of 10. But for me, thats what i wanted and spent sometime searching for this code and would like to thank him and give atleast 8 out of 10.

Feed for this site 'codefight.org' is available at the bottom or you can click here.

I haven't currently added this to the download file. But if you want this, follow the instruction below:

STEP 1: [frontend/controllers/feed.php]
class Feed extends Controller
{
function Feed()
{
parent::Controller();
$this->load->model('page_model', '', TRUE);
$this->load->helper('xml');
}
function index()
{
$data['encoding'] = 'utf-8';
$data['feed_name'] = 'CodeFight.org';
$data['feed_url'] = 'http://www.codefight.org';
$data['page_description'] = 'Code Fight, php, and the World of CMS';
$data['page_language'] = 'en-au';
$data['creator_email'] = 'Damodar Bashyal is at enjoygame at hotmail dot com';
$data['posts'] = $this->page_model->getRecentPosts();
header("Content-Type: application/rss+xml");
$this->load->view('feed/rss', $data);
}
}

STEP 2: [frontend/view/feed/rss.php]
<?php
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>

<title><?php echo $feed_name; ?></title>
<link><?php echo $feed_url; ?></link>
<description><?php echo $page_description; ?></description>
<dc:language><?php echo $page_language; ?></dc:language>
<dc:creator><?php echo $creator_email; ?></dc:creator>
<dc:rights>Copyright <?php echo gmdate("Y", time()); ?></dc:rights>
<admin:generatorAgent rdf:resource="http://www.codefight.org/" />
<?php foreach($posts->result() as $entry):
$link = $this->data_model->link_clean($entry->pages_title);
$menu_id = explode(',',$entry->menu_id);
$menu_id = array_filter($menu_id);
$menu_id = array_shift($menu_id);
$link = "page/$menu_id/{$entry->pages_id}/$link";
?>
<item>
<title><?php echo xml_convert($entry->pages_title); ?></title>
<link><?php echo site_url($link) ?></link>
<guid><?php echo site_url($link) ?></guid>
<description><![CDATA[
<?= preg_replace('#\[code\](.+)\[\/code\]#isU','&lt;code&gt; &lt;?php ... ?&gt; &lt;/code&gt;',$entry->pages_blurb); ?>
]]></description>
<pubDate><?php echo date ('r', strtotime($entry->pages_date));?></pubDate>
</item>

<?php endforeach; ?>

</channel></rss> 

STEP 3: [add below to: frontend/models/page_model.php]
function getRecentPosts ($limit = 10)
{
$this->db->orderby('pages_id', 'desc');
$this->db->where('pages_active', 1);
$this->db->limit($limit);
return $this->db->get('pages');
}

All Done!!!

How to access your new feed? just goto yoursite/feed.html

This page is too long so lets create new page for sitemap.

Beta Tester posted on - Wednesday 1st of April 2009 05:30:20 AM

Thanks. Successfully added.
 
not published on website


QR Code: Feed and sitemap generation Part I