Welcome Guest, Not a member yet? Register   Sign In
regex problem in routes.
#1

[eluser]Zeeshan Rasool[/eluser]
As i found in CI manual
Code:
$routes['funcOne/:any'] = "site/funcOne/$1"
it means there can be any type of param in the end but i found that we can also use
Code:
(.*)
in place of
Code:
:any
so that it also works but i had a problem in my blog.
i have same routes file for admin and site to control url for site i write something like that
Code:
$routes['(.*)/funcTwo'] = "site/funcOne/"
my question is, that if i have a link like this

site/second/funcTwo
the above command also executes for this. It means (.*) assumes first two segments site/second as a single and redirects it to site/funcOne
why?
Thanks
#2

[eluser]rogierb[/eluser]
(.*) is a regex that gets everything. So it does not matter if there are one, two or three segment. It just allows for everyting.

Edit: try something like ([a-z]*){1}/funcOne
#3

[eluser]Zeeshan Rasool[/eluser]
thanks ! you mean if i use .*/funcOne then it assumes all param single that come before funcOne?
i-e
if we have
Code:
$routes["(.*)/funcOne"] = site/funcOne;
then
Code:
site/test/funcOne
site/test/test2/funcOne
site/funcOne

above all three function will go to site/funcOne as we given?
#4

[eluser]rogierb[/eluser]
Quote:above all three function will go to site/funcOne as we given?

Yeah, it does.
#5

[eluser]Zeeshan Rasool[/eluser]
Thanks alot ! really helpfull
#6

[eluser]BrianDHall[/eluser]
For extra regex help on this sort of thing: http://www.regular-expressions.info/

You might consider a regex like this: [^/]+/funcOne(.*)

That means "everything that is not /, as many as you can get, which is to be then immediately followed by /funcOne...then grab everything you can after that".

This will work on anything with something/funcOne anywhere in the url and redirect it to site/funcOne.

However, wouldn't you want parameters to that function redirect as well? Then perhaps this:

Code:
$routes["[^/]+/funcOne(.*)"] = "site/funcOne/$1";

The $1 holds anything caught inside the first set of parens and sticks it on the end of the reroute.

One 'extra feature' is that this rule would still reroute a request like /test/controller/site/funcOnedontreroute and reroute it as site/funcOne/dontreroute

Other than that it's perfectly alright Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB