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.
#2

[eluser]louis w[/eluser]
I really like this approach, I am trying to solve a similar problem. I would like clean(er) urls, but still allow for the flexibility that CI comes with.

I am wondering something thou, I do not see _validate_segments in CI_Router.php. Am I missing something here, shouldn't this be in there if you are overwriting it, and then calling back to it in your else.

Sorry I do not have answers to your questions, I wonder them myself too.
#3

[eluser]zauber[/eluser]
Apparently the function I am overriding has changed to "_validate_request" in version 1.6. The same code still works, but you need to change "_validate_segments" to "_validate_request".
#4

[eluser]louis w[/eluser]
Ah ha! Thanks Smile
#5

[eluser]WinkingFrog[/eluser]
This is exactly the kind of thing I am after for a couple of projects just now, but can anyone tell me if there have been any changes to the core since this was originally posted that may effect the operation/function?

Is there another way of achieving this that is now wider known?

Am I barking up the wrong tree? Smile
#6

[eluser]darkhouse[/eluser]
Just putting in my 2 cents. One Issue I see here is dealing with multiple segments. I dealt with this a while ago and here's my solution.

http://ellislab.com/forums/viewthread/10...15/#532408

It works very well, using it on sites like http://www.ochc.ca. You'll notice that we have pages like http://www.ochc.ca/programs and http://www.ochc.ca/programs/trust_fund. Multiple segments. This site also has a custom news system, http://www.ochc.ca/news/article/23 for instance. Sure I could've made this work directly in the CMS, but the CMS is still in its early stages and doesn't support anything other than a title and text area. I need to create modules so that you can choose different content types... but that's another story.

Point is, I was able to have some custom development along with some content management, both with multiple segments, and the router knows which is which.

It may not be the most glamorous solution, but it works, and works well.
#7

[eluser]WinkingFrog[/eluser]
Nice one, thanks for the response!

I'll gladly take a look at the thread you linked, it sounds very much like you are already achieving what I am trying to...

Thanks again




Theme © iAndrew 2016 - Forum software by © MyBB