-
Notifications
You must be signed in to change notification settings - Fork 0
Google Sitemaps
[h2]Introduction[/h2] I have whipped up a very simple and easy-to-use library for automatically building sitemaps for your CodeIgniter web application. [h2]Requirements[/h2] [h3]CodeIgniter[/h3] Of course. Built on version 1.53. [h3]Extended Parser class[/h3] I created an extension of the parser class, which simply adds a method called sparse() which will allow you to parse a template stored as a string variable instead of a file. You can get the extended library below. [h2]Setup[/h2] Just copy and paste both of the libraries below into your application/libraries folder: [h3]Sitemap.php[/h3] [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
- A very simple sitemap link generator
- This class uses the PHP5 Reflection class to find all the public methods
- in a CodeIgniter controller class and generate a list of links.
- @author Jonathon Hill <jonathon@compwright.com>
- @license CodeIgniter license
- @requires MY_Parser extended Parser class [added sparse() for parsing templates stored in a string var]
- @requires Built for CodeIgniter 1.6
*/ class Sitemap {
/**
* CodeIgniter base object reference
*
* @var object
*/
private $CI;
/**
* Sitemap links template
*
* @var string
*/
private $template = '<strong><a href="{section_index}">{section_text}</a></strong>[ul]{links}[li]<a href="{link_url}">{link_text}</a>[/li]{/links}[/ul]';
/**
* Method names to ignore
*
* @var array
*/
private $ignore = array(
'get_instance',
'__construct',
'Controller',
'_ci_initialize',
'_ci_scaffolding',
'CI_Base'
);
/**
* Sitemap object initialization
*
*/
function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('parser');
}
/**
* Set a template for the sitemap links
*
* @param string $tpl
*/
function set_template($tpl)
{
$this->template = $tpl;
}
/**
* Build the sitemap links
*
* @param string $page (optional) Build all the links for a specific controller
* @return string
*/
function get_links($class)
{
// Use the PHP5 Reflection class to introspect the controller
$controller = new ReflectionClass($class);
$data['links'] = array();
$data['section_index'] = site_url($class);
$data['section_text'] = ucwords(strtr($class, array('_'=>' ')));
foreach($controller->getMethods() as $method)
{
// skip CI system methods
if(in_array($method->name, $this->ignore)) continue;
// skip methods that begin with '_'
if(substr($method->name, 0, 1) == '_') continue;
// skip methods that aren't public
if(!$method->isPublic()) continue;
// build link data for parser class
$data['links'][] = array(
'link_url' => site_url("$class/$method->name"),
'link_text'=> ucwords(strtr($method->name, array('_'=>' '))),
);
}
return $this->CI->parser->sparse($this->template, $data, true);
}
}
?> [/code] [h3]MY_Parser.php[/h3] [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------
/**
-
Sparser Class
-
@package CodeIgniter
-
@subpackage Libraries
-
@category Parser
-
@author Jonathon Hill
-
@link jhill@goyoders.com */ class MY_Parser extends CI_Parser {
/**
-
Parse a string
-
Parses pseudo-variables contained in the specified string,
-
replacing them with the data in the second param
-
@access public
-
@param string
-
@param array
-
@param bool
-
@return string */ function sparse($template, $data, $return = FALSE) { $CI =& get_instance();
if ($template == '') { return FALSE; }
foreach ($data as $key => $val) { if (is_array($val)) { $template = $this->_parse_pair($key, $val, $template);
} else { $template = $this->_parse_single($key, (string)$val, $template); } }if ($return == FALSE) { $CI->output->final_output = $template; }
return $template; }
-
} // END Sparser Class ?> [/code] [h2]Usage example[/h2] [code] $this->load->library('sitemap'); echo $this->sitemap->get_links('utility'); // pass the name of the controller you want [/code] [h2]Room for improvement[/h2] This library can use lots of improvement for some of the things it doesn't do:
- Work with controllers other than the current controller
- Build a sitemap for the whole site
- Original author: Derek Jones
- How to extend helpers: See User Guide
- Modified by: Thomas Stapleton (id, classes, selected country option and all option)
- Modified by: Bradley De-Lar (construct, setLayout example)