CodeIgniter Custom Admin URI Routes
UPDATE: SOLVED! For the broken pages I simply made an admin controller. That has a function for each model now :) Happy days!
Trying to house my admin function in the same controller as my front-end code. To do this I am setting up some custom routes so that admin can be accessed via:
/admin/controller/id // instead of /controller/admin/id
/admin/controller/create // instead of /controller/create
/admin/controller/detail/id // instead of /controller/detail/id
/admin/controller/update/id // instead of /controller/update/id
/admin/controller/delete/id // instead of /controller/delete/id
My current routes work perfectly for detail, create, update, delete
$route['admin/(:any)/detail'] = "$1/detail"; // WORKS!!!
$route['admin/(:any)/detail/(:num)'] = "$1/detail/$2"; // WORKS!!!
$route['admin/(:any)/create'] = "$1/create"; // WORKS!!!
$route['admin/(:any)/create/(:num)'] = "$1/create/$2"; // WORKS!!!
$route['admin/(:any)/update'] = "$1/update"; // WORKS!!!
$route['admin/(:any)/update/(:num)'] = "$1/update/$2"; // WORKS!!!
$route['admin/(:any)/delete'] = "$1/delete"; // WORKS!!!
$route['admin/(:any)/delete/(:num)'] = "$1/delete/$2开发者_开发问答"; // WORKS!!!
HOWEVER I cannot get the admin page to work with an ID. I can reroute the index page, but will be unable to use pagination if I cannot pass the ID.
$route['admin/(:any)'] = "$1/admin"; // WORKS!!!
$route['admin/(:any)/(:num)'] = "$1/admin/$2"; // EPIC FAIL :( 404's
Can someone please help me solve this problem, or even suggest an alternative solution of application structure.
Make sure your regex routes (that includes routes with :num and :any) must go AFTER the default_controller route.
For more ways to create a decent admin panel, take a look at my article on creating an admin panel in CodeIgniter. There are three decent ways to do it, this article explains the pro's and con's of each.
If your admin is complicated and you need several controllers for it, you can simply create an admin folder in your application/controllers folder. Then put your admin controllers in that.
So application/controllers/admin/login would work like http://example.com/admin/login
Old question that already has an accepted answer, but I think your setup would work fine if you included an end-of-line anchor in your first RegEx:
$route['admin/(:any)/(:num)$'] = "$1/admin/$2"; // EPIC FAIL :( 404's
^^^
Otherwise URLs you're intending to capture w/ your second RegEx will get intercepted by your first RegEx.
精彩评论