[eluser]zauber[/eluser]
Hi, here's a small contribution that some might find useful.
I'm working on a really simple and sweet CMS with CodeIgniter, which handles pages that basically just have content and slugs (a string that only contains alphanumerics and underscores)
when you visit
Code:
http:://www.mysite.com/pages/read/some_slug
then the task of Pages->read($slug) would be to show the content of the page with slug 'some_slug'.
now... I wanted to be able to enter:
Code:
http:://www.mysite.com/some_slug
and get the same effect. But I wanted to do it in a way that it wouldn't disturb any other routes. (Simply adding a $route[:any] = "pages/read", didn't work.
I solved it by extending the Router class, and allowing another special $route index.
Heres the Router extension:
Code:
class MY_Router extends CI_Router{
function _validate_segments($s){
if (count($s)==1 && !file_exists(APPPATH.'controllers/'.$s[0].EXT)){
if (isset($this->routes['default_query'])){
$segments = explode("/",$this->routes['default_query']);
$segments[] = $s[0];
$this->set_class($segments[0]);
$this->set_method($segments[1]);
return $segments;
}
show_404();
}
else return parent::_validate_segments($s);
}
}
The special $route index is 'default_query' and it work so that if you put in:
Code:
$route['default_query'] = "mycontroller/amethod"
then any requests like:
Code:
http://mysite.com/writesomethinghere
will execute as: MyController->amethod('writesomethinghere');
My questions are:
1) Is there a more direct 'built-in' way of achieving this that I have overlooked?
2) Does this punch any holes in security or break functionality in some way I have yet to notice?
3) Is this the most efficient (computing-wise) way of adding this feature, without hacking the Router class directly? I tried to avoid as much repetition of code in the Router class as possible, and adding as little new code as possible myself, but I'm not sure how well I succeeded.