Welcome Guest, Not a member yet? Register   Sign In
Using (:any) in routes
#1

[eluser]mddd[/eluser]
Because of this thread I found that (:any) behaves differently then I expected. And maybe differently then other people might expect.

Normally, routes have to match exactly: $route['method/controller'] will NOT match "method/controller/123".
Also, $route['method/controller/(:num)'] WILL match "method/controller/123" but NOT "method/controller/123/456".

On the other hand, $route['method/controller/(:any)'] will match "method/controller/test" but ALSO "method/controller/test/another-test".

From reading the manual, it seems that (:any) is meant to match any segment. But in reality, it matches any segment or multiple segments. This caused the problem for the topic starter in the thread mentioned before.

It seems to me that :any should really be translated into [^/]+ and not .+. Although I realize it breaks previous behaviour.
What do you think?
#2

[eluser]yohanip[/eluser]
IMO the name depicts for the functions.. (any means anything)
Wink
it's up to the programmer to get accustomed to the original function or should the programmer took considerations when he/she decided to user the $route[.../.../.../.../(:any)]
#3

[eluser]mddd[/eluser]
Of course it is the responsibility of the programmer to know what he/she is doing.But I do think it can be unclear, even from reading the manual, what :any actually does.

If someone wrote $route['(:any)/(:num)/(:num)'] it would be reasonable for them to expect that this would only match something like abc123/456/789 and not somecontroller/somemethod/some-other-value/123/456.

I think this is because :any appears in the manual only in the context of 1 segment. Maybe we should expand the manual to give some examples like $route['(:any)'] will match /hallo, /123, /123/456 etc.

And also it could be useful to have a separate shortcut for the 'simple' behaviour of matching a single segment. Something like :word to match any word, including numbers, letters and other characters. Something like that.
#4

[eluser]jabbett[/eluser]
A related scenario:

Code:
$route['controller/:any'] = "controller/index";

This matches visits to controller as well as controller/1234.

However, in the latter case, the index method never receives any parameters. They're all null.

I tried using $1 like this:

Code:
$route['controller/:any'] = "controller/index/$1";

...but then my parameter gets set literally as "$1".

How do I perform the custom route and preserve all subsequent URI segments?

Thanks!
#5

[eluser]jabbett[/eluser]
Just noticed I wasn't using parentheses around my :any.

Code:
$route['controller/(:any)'] = "controller/index/$1";

...does the trick!




Theme © iAndrew 2016 - Forum software by © MyBB