Skip to content

Instantly share code, notes, and snippets.

@j-mcnally
Created August 18, 2011 01:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j-mcnally/1153064 to your computer and use it in GitHub Desktop.
Save j-mcnally/1153064 to your computer and use it in GitHub Desktop.
CodeIgniter Permalink Routing
This is a bit of contrived example because it relies on our own style of page hierarchies
but perhaps this will give you a stepping stone to apply your own hierarchical page
structure.
Also this example uses DataMapper for ORM in case the model calls were confusing you.
<?php
//would be controllers/pg.php
class pg extends Controller {
var $pgItem;
var $parentPage;
function pg() {
//do some constructor related stuff here
}
function permalink($segments) {
foreach($segments as $seg) {
//find a child of the page where permalink matches the next segment
if ($this->pageParent == null) {
$part = new Page();
$part = $part->get_by_permalink($seg);
}
else {
$part = new Page();
$part = $part->where('parentId', $this->pageParent->id)->where('permalink', $seg)->get();
}
if ($part->id == null) {
$this->e404();
die();
}
else {
unset($this->pageParent);
$this->pageParent = $part;
}
echo $this->pageParent->permalink;
unset($part);
}
$this->pgItem = $this->pageParent;
unset($this->pageParent);
echo $this->pgItem->title; //at this point we have the page
}
function index() {
$this->permalink(func_get_args());
die();
}
}
?>
<?
//would be config/routes.php
$route['default_controller'] = "pg";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment