CodeIgniter Forums

Full Version: Default Method for Controllers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
I put my controllers in subfolders and I'm trying to modify the routes to set a default method for all controllers. Here is what I have:

Code:
$route['(:any)'] = "$0/dashboard";

This works, until I try and access any method other than dashboard, then it gives me a 404.

El Forum

[eluser]Nick Husher[/eluser]
Because it's probably looking for whatever your function name is/dashboard.

So if you were to look for /blog/archive, your router is translating that to /blog/archive/dashboard. Not really what you want.

What you probably want to do is take a look at the CI_Router class and extend it with a MY_Router. I'm 85% sure that's where the default method for controllers is set to "index"

El Forum

[eluser]jdfwarrior[/eluser]
With routes, if you define a default route, it takes precedence over the other routes, which is what its supposed to do. If you use (:any), you have to manually create routes to everything else. They have to be created above the (:any) route in the route.php as well.

For instance:
Code:
$route['blog'] = "blog";

$route['(:any)'] = "$0/dashboard";

Basically goes right down the list, if it finds it before (:any), it goes where it should, if it doesnt find it, it considers (:any) as an, "if all else fails, do this"

El Forum

[eluser]brianw1975[/eluser]
er....

Code:
$route['(:any)'] = "$0/dashboard";

call me crazy but shouldn't $0 be $1 ?

El Forum

[eluser]jdfwarrior[/eluser]
[quote author="brianw1975" date="1252020556"]er....

Code:
$route['(:any)'] = "$0/dashboard";

call me crazy but shouldn't $0 be $1 ?[/quote]

I was wondering the same thing but he said it was working, so i just skipped that part and went on..

El Forum

[eluser]Chad Fulton[/eluser]
$0 matches the entire string that matched the regex pattern, and since the pattern is (:any), $0 is the same as $1 in this case. If the pattern was index/(:any), then $0 would be (for example) "index/dashboard", whereas $1 would be "dashboard".