Welcome Guest, Not a member yet? Register   Sign In
Can Only Use One Regular Expression In URI Routing?
#1

[eluser]Bob Dog[/eluser]
You need to change your documentation of URI Routing to mention the fact that you can only use one regular expression. I learned this the hard way after tearing my hair out for a couple of hours trying to figure out why my set of regular expressions did not work.

For example, suppose you have a URI with the following segments:

    /something/index/seg1/seg2/seg3/

and you try to use these two regular expressions:

    $route['(.*)/seg1/(.*)'] = "$1/newseg1/$2";
    $route['(.*)/seg2/(.*)'] = "$1/newseg2/$2";

then instead of getting what you would expect:

    /something/index/newseg1/newseg2/seg3/

you actually get:

    /something/index/newseg1/seg2/seg3/

The second regular expression is simply ignored. Either one in isolation works, but put together, only the first one gets applied. For me, at least, this was very unexpected and confusing behavior. In the documentation is this note:

    "Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones."

I don't know if this statement was intended to document the odd behavior, but if so, it was completely unclear to me. In fact, I still don't know what it means.

Or maybe this is a bug and has since been fixed? I'm using V1.7.3.

Thanks!
#2

[eluser]John_Betong_002[/eluser]
Take a look at:

http://ellislab.com/forums/viewthread/207760/#966964

I think that you are trying to achieve what the routing library was not designed for.

I added these lines to my routes.php and also the results.

Code:
# Required: Item#1
  # http://localhost/something/index/seg1/seg2/seg3/
    $route["(:any)/index/seg1/seg2/seg3"]     = "welcome/tom/$1";
  # result:
  # $this->uri->uri_string()  -> something/index/seg1/seg2/seg3
  # Method:   Welcome::tom  
  
  # Required: Item#2
    # http://localhost/something/index/seg2/seg3/seg4/
    $route["(:any)/index/seg2/seg3/seg4"]     = "welcome/pauline/$1";
  # result:
  # 404 Page Not Found

# When Item#2 is disabled everything on my site works and the final route is activated
  # finally the Daddy of them all
  $route["(:any)"]                    = "controller_search/index/$1";
 
 
edit: I have just checked and echo count($route) = 157
 
 





#3

[eluser]CroNiX[/eluser]
On a large old 1.7.3 project, I have routes like:
Code:
$route['(.*)/some_constant/(.*)/some_other_constant/(.*)'] = 'controller/method/$1/$2/$3';
And all 3 captured segments are properly passed...

#4

[eluser]Bob Dog[/eluser]
I'm not sure how these two replies address my comment. I was simply pointing out that when you have two regular expression route mappings, then the second one seems to be ignored. This is counter-intuitive and ought to be documented. My code above was contrived to demonstrate the effect.

Unless I'm misunderstanding something fundamental about how these route mappings are supposed to work. If so, then perhaps the URI Routing documentation needs to be updated to explain it better, because I didn't get it despite multiple readings.
#5

[eluser]CroNiX[/eluser]
My comment was to state that I have it working passing all 3 parameters, in 1.7.3.




Theme © iAndrew 2016 - Forum software by © MyBB