Welcome Guest, Not a member yet? Register   Sign In
routing rules- what am I doing wrong?
#1

[eluser]vivid_haze[/eluser]
Please take a look at what's in my codeigniter config/routes.php file:

Code:
$route['default_controller'] = "primary";
$route['404_override']    = '';

$route['(:any)']              = 'primary';
$route['login']               = 'login';
$route['admin']               = "admin";
$route['admin/(:any)']    = 'admin/$1';

So, I want pretty much any page request to be directed to my 'primary' controller, where it will be taken care of by the index() function there. There are a few exceptions; login goes to login, admin to admin. That's all working fine. The problem lies in the last rule, which doesn't seem to work as it should; 'admin/whatever' just gets routed back to my primary controller. Why?

I would actually like to route any admin requests normally, so admin/whatever/ goes to admin/whatever/, and admin/whatever/whatever/ goes to admin/whatever/whatever/. There could be many segments, and there are too many to list individual rules (exceptions to the first $route['(:any)'] = 'primary'; rule). Is there a way to do this?

ie. Any url with the first segment 'admin' gets routed to the admin controller (just as codeigntier would work normally). However, this needs to be stated as an exception to the first $route['(:any)'] rule?

I'm fairly new to codeigniter, so apologies if answers are glaringly obvious. I've spent the last few hours digging around for answers though, and can't find any.
#2

[eluser]CroNiX[/eluser]
Rules run until a match is found. You have a catchall route (:any) before your login and admin routes, so the route will always go to your primary controller and never reach your other routes since that will get matched first. Put your (:any) route very last so that the other routes can be caught first, and then if they don't it will use your catchall route.

Also, you should order your routes that have the most segments first. Your 'admin/(:any)' route should come before the plain 'admin' route.
#3

[eluser]vivid_haze[/eluser]
Ah! How stupid. Thank you, sir!





Theme © iAndrew 2016 - Forum software by © MyBB