[SOLVED] Reduce the number of routing rules? |
[eluser]JayTee[/eluser]
I have a set of routing rules that looks like this: Code: $route['(:any)/(:any)/(:any)/(:any)'] = "$2/$3/$4"; The goal is to make sure CI always ignores segment 1 (I'm using that segment for something special) Is there any way to always ignore segment 1 regardless of how many segments occur after it without having to make lots of different routes like I had to do above? SOLVED: - my last response
[eluser]WanWizard[/eluser]
Code: $route['(:any)/(.*)'] = "$2";
[eluser]JayTee[/eluser]
Didn't work quite right: Code: $route['(:any)/(.*)'] = "$2"; http://example.com/blah/controller/method -> 404 not found http://example.com/blah/controller/method/arg1 -> 404 not found It seems like the second capture group only focuses on segment 2 and ignores segments 3...N
[eluser]WanWizard[/eluser]
My fault, the slash is seen by the route parser as a URI element, so it must be Code: // with the slash Code: // without the slash
[eluser]JayTee[/eluser]
It turns out that, like WanWizard mentioned, RegEx is definitely the way to go. The issue is the (:any) part of the RegEx. CodeIgniter's Router class converts ':any' to '.+' - which matches 1 or more of any character. So for this rule: Code: $route['(:any)/(.*)'] = "$2"; "Match 1 or more of any character followed by a '/' followed by 0 or more of any character" http://example.com/blah/controller -> $2 = 'controller' http://example.com/blah/controller/method -> $2 = 'method' (404 error) http://example.com/blah/controller/method/arg -> $2 = 'arg' (404 error) The issue is because .+ matches everything before the last literal '/' character ('blah/controller' in the second url I listed) To fix this, I changed the first part of the rule so that it only matches the 'word' characters of the first segment: Code: $route['(\w+)/(.*)'] = $2; "Match 1 or more word characters followed by a '/' followed by 0 or more of any character" By sticking to only "word" characters (A "word" character is any letter or digit or the underscore character), it doesn't match the first '/', so the rest of the rule works. And with that single routing rule, I am able to ignore the first segment if all the URI's for my site. |
Welcome Guest, Not a member yet? Register Sign In |