Skip to content

Instantly share code, notes, and snippets.

@mrkodssldrf
Created September 26, 2012 08:19
Show Gist options
  • Save mrkodssldrf/3786756 to your computer and use it in GitHub Desktop.
Save mrkodssldrf/3786756 to your computer and use it in GitHub Desktop.
Update PyroCMS Core Sitemap to generate a Sitemap
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Sitemap module public controller
*
* Renders a human-readable sitemap with all public pages and blog categories
* Also renders a machine-readable sitemap for search engines
*
* @author Barnabas Kendall <barnabas@bkendall.biz>
* @license Apache License v2.0
* @version 1.1
* @package PyroCMS\Core\Modules\Sitemap\Controllers
*/
class Sitemap extends Public_Controller {
/**
* XML method - output sitemap in XML format for search engines
*
* @return void
*/
public function xml()
{
$this->load->model('blog/blog_m');
$this->load->model('navigation/navigation_m');
$doc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" />');
// first get a list of enabled modules, use them for the listing
$modules = $this->module_m->get_all(array('is_frontend' => 1));
foreach ($modules as $module)
{
// To understand recursion, you must first understand recursion
if ($module['slug'] == 'sitemap')
{
continue;
}
if ( ! file_exists($module['path'].'/controllers/sitemap.php'))
{
continue;
}
}
$articles = $this->blog_m->get_many_by(array('status', 'live'));
foreach($articles as $article) {
$node = $doc->addChild('url');
$loc = site_url('blog/'.date('Y/m/', $article->created_on).$article->slug, $article->title);
$node->addChild('loc', $loc);
if($article->updated_on) {
$node->addChild('lastmod', date(DATE_W3C, $article->updated_on));
}
}
$this->output
->set_content_type('application/xml')
->set_output($doc->asXML());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment