Welcome Guest, Not a member yet? Register   Sign In
Default Method for Controllers

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:

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

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

[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"

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:
$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"


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

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

[quote author="brianw1975" date="1252020556"]er....

$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..

[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".

Theme © iAndrew 2016 - Forum software by © MyBB