Created
August 18, 2011 01:13
-
-
Save j-mcnally/1153064 to your computer and use it in GitHub Desktop.
CodeIgniter Permalink Routing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
//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