Simple sitemaps in codeigniter
22 Sep 2011Creating sitemaps is a bit of a hassle for any site bigger than a couple of pages, especially where a database is involved. While working on a recent project I created a library for codeigniter which allows the simple creation of sitemaps either by manually adding pages which might be appropriate for a blog, or allowing the library to scan controllers and adding all pages defined by methods. Common search engines can also be notified.
A simple example of it in use is shown below:
<?php
//if you are using sparks
$this->load->spark('codeigniter-sitemaps/0.0.1')
$this->load->library('sitemaps');
//assuming a hypothetical posts_model
$posts = $this->posts_model->get_posts();
foreach ($posts AS $post)
{
$item = array(
'loc' => site_url('blog/' . $post->slug),
'lastmod' => date('c', strtotime($post->last_modified)),
'changefreq' => 'hourly',
'priority' => '0.8'
);
$this->sitemaps->add_item($item);
}
// file name may change due to compression
$file_name = $this->sitemaps->build('sitemap_blog.xml');
$reponses = $this->sitemaps->ping(site_url($file_name));
?>
You can download the spark from: http://getsparks.org/packages/codeigniter-sitemaps
Or get the code from my github: http://github.com/chemicaloliver/codeigniter-sitemaps
Simple documentation is included and should be self explanatory.
This library is based on work by Philipp Dörner 2010 http://signalkraft.com/google-sitemaps-for-codeigniter