CodeIgniter Forums
Routing and Remap - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Routing and Remap (/thread-27311.html)



Routing and Remap - El Forum - 02-07-2010

[eluser]JanDoToDo[/eluser]
Hey guys,
I'm building a CMS with a Public_Controller and an Admin_Controller both extended from MY_Controller. I have a few controllers which are public facing that serve different functions which is why i made them different controllers, e.g. users, auth, pages etc and each of these are childs of Public_Controller. This is similar to the situation i have with my Admin controllers.

My questions is, I route using the routing rules to the specified controller required, but im wondering if it is better to just route to the particular function you need in the controller or, use the _remap method in the controller to remap the request? I'm wondering because, if the URI is say "users/edit/profile/3" then the routing rules know to route this to users instead of say pages or auth and go to the right place, but does the remap function also keep the subsequest uri segments in the request? I guess using remap is better because then i could go to a function depending on a setting in the database or someting which i couldnt do in the routing rules?

Is this the best way to go about it? My current routing rules are:

Code:
$route['admin/(:any)'] = 'admin/$1';
$route['admin'] = 'admin/pages';

$route['auth/(:any)'] = 'auth/$1';
$route['auth'] = 'auth/index';
$route['(:any)'] = "page";

Do these seem right?! Will the (:any) in the rules take account of multiple sub sections? like admin/create/user/profile/2? If a method isnt in the remap function but gets called will it just run its default route, i.e. the method name?


Routing and Remap - El Forum - 02-07-2010

[eluser]JanDoToDo[/eluser]
Hey guys any thoughts? Smile


Routing and Remap - El Forum - 02-07-2010

[eluser]tomcode[/eluser]
I use one schema for the routes:

Code:
$route['default_controller'] = 'pages';

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

$route['auth/(:any)'] = 'auth/$1';
$route['auth'] = 'auth';

This gives me one common way how to build the controllers and yes, all uri segments are preserved.

Right now I work with a common _remap() in MY_Controller which I still can override.


Routing and Remap - El Forum - 02-07-2010

[eluser]JanDoToDo[/eluser]
Tomcode, forgive me but it doesn't look like you are routing every page request to a default controller? Doesn't the "default_controller" route only get called when there ar no uri segments?


Routing and Remap - El Forum - 02-07-2010

[eluser]tomcode[/eluser]
You're right, I forgot one line,

Code:
$route['default_controller'] = 'pages';

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

$route['auth/(:any)'] = 'auth/$1';
$route['auth'] = 'auth';

$route['(:any)'] = "pages/$1";



Routing and Remap - El Forum - 02-07-2010

[eluser]JanDoToDo[/eluser]
Ah... ok.. So you use the same rules as me??! Smile Thats cool!


Routing and Remap - El Forum - 02-07-2010

[eluser]tomcode[/eluser]
Yeah, that's why I couldn't resist to answer Wink .

In fact, in the above code sample I've adapted mine to match Your controller names, also I have a multi-language site, here is what I actually use :

Code:
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

$route['([a-z]+)/connection'] = 'connection';
$route['([a-z]+)/connection(:any)'] = 'connection/$1';

$route['edit'] = 'edit';
$route['edit(:any)'] = 'edit/$1';

$route['(:any)'] = "welcome/$1";

Where edit is the central ajax controller for the jquery.jeditable calls (I use exclusively inline editing for the page contents).


Routing and Remap - El Forum - 02-07-2010

[eluser]darkhouse[/eluser]
I actually posted about this last year, here [url="http://ellislab.com/forums/viewthread/107451/#541043"]http://ellislab.com/forums/viewthread/107451/#541043[/url] and I'm still using this code today.

I just don't think the CMS should take every request, I think it should be the last resort. This system allows you to have custom pages (or just have content management for a couple of your pages) and you don't have to create a route for each one. It checks to see if the file exists first, and if not then hand it over to the CMS to deal with. The CMS from there will take the URI, see if there's a corresponding page in the database, and load it up if there is. If it doesn't exist, it shows a 404 like it would normally.

It has come in quite handy for a couple of things, but the main thing was the CMS that still allowed custom pages. I'm using it right now for a hotel site, they wanted to control most of their content, but then also have an online booking system that works with their internal software. Sure I could've made a route for just one controller (actually it would've been a number of routes as it has subpages), but as I said before I think the CMS should come last, not first, and this way I don't have to worry about creating routes for items I shouldn't need to create routes for in the first place.