Welcome Guest, Not a member yet? Register   Sign In
default request by extending Router
#1

[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.


Messages In This Thread
default request by extending Router - by El Forum - 02-16-2008, 08:30 PM
default request by extending Router - by El Forum - 03-04-2008, 09:10 PM
default request by extending Router - by El Forum - 03-05-2008, 12:38 AM
default request by extending Router - by El Forum - 03-05-2008, 06:47 AM
default request by extending Router - by El Forum - 07-05-2009, 01:57 AM
default request by extending Router - by El Forum - 07-05-2009, 07:30 AM
default request by extending Router - by El Forum - 07-05-2009, 08:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB