Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 13 revisions

[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 = '&lt;strong&gt;&lt;a href="{section_index}"&gt;{section_text}&lt;/a&gt;&lt;/strong&gt;[ul]{links}[li]&lt;a href="{link_url}"&gt;{link_text}&lt;/a&gt;[/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-&gt;CI =& get_instance();
    $this-&gt;CI-&gt;load-&gt;library('parser');
}


/**
 * Set a template for the sitemap links
 *
 * @param string $tpl
 */
function set_template($tpl)
{
    $this-&gt;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('_'=&gt;' ')));
    
    foreach($controller-&gt;getMethods() as $method)
    {
        // skip CI system methods
        if(in_array($method-&gt;name, $this-&gt;ignore)) continue;
        
        // skip methods that begin with '_'
        if(substr($method-&gt;name, 0, 1) == '_') continue;

        // skip methods that aren't public
        if(!$method-&gt;isPublic()) continue;
        
        // build link data for parser class
        $data['links'][] = array(
            'link_url' =&gt; site_url("$class/$method-&gt;name"),
            'link_text'=&gt; ucwords(strtr($method-&gt;name, array('_'=&gt;' '))),
        );
    }
    
    return $this-&gt;CI-&gt;parser-&gt;sparse($this-&gt;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

Clone this wiki locally