CodeIgniter Forums
Routing question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Routing question (/showthread.php?tid=23442)



Routing question - El Forum - 10-11-2009

[eluser]Russ Back[/eluser]
Hi, just started working with CI and have a quick question.

I'd like to remap all urls that aren't /manage/:any or /contact/:any to /page/$1/$2/whatever

Is there an easy way to do this? In other words urls like /contact, /contact/thanks, /about-us, /about-us/me should route to the page controller: /page/contact, /page/contact/thanks, etc.


Routing question - El Forum - 10-14-2009

[eluser]markup2go[/eluser]
If page is the controller then you could set 'page' as your default controller $route['default_controller'] = "page"; could you not?


Routing question - El Forum - 10-14-2009

[eluser]Russ Back[/eluser]
That's what I thought but the minute you add to the url (/about-us/ for example) it attempts to load the about-us controller when I want it to be routed to the page controller with about-us passed as a uri segment


Routing question - El Forum - 10-14-2009

[eluser]BrianDHall[/eluser]
Here you go:

Code:
$route['default_controller'] = "defaultcontroller";

// Route everything but mentioned controllers to default controller, allowing short urls to work.
$route['^(?!controller|controller|controller).*'] = $route['default_controller'] . "/$0";

In filled out for you:

Code:
$route['default_controller'] = "page";

// Route everything but mentioned controllers to default controller, allowing short urls to work.
$route['^(?!manage|contact).*'] = $route['default_controller'] . "/$0";

I use the same method, so unless there is another change I'm forgetting about that should work.


Routing question - El Forum - 10-14-2009

[eluser]Russ Back[/eluser]
That's perfect - thanks a lot!


Routing question - El Forum - 11-20-2009

[eluser]Russ Back[/eluser]
Just noticed a problem with this.

'manage' shouldn't resolve to 'page' but 'management' should. But this doesn't achieve this - 'management' attempts to load a 'manangement' controller.

Any thoughts?

Thanks again


Routing question - El Forum - 11-20-2009

[eluser]BrianDHall[/eluser]
[quote author="russback" date="1258773471"]Just noticed a problem with this.

'manage' shouldn't resolve to 'page' but 'management' should. But this doesn't achieve this - 'management' attempts to load a 'manangement' controller.

Any thoughts?

Thanks again[/quote]

Tricky!

You'll need a little bit more fancy of a route then:

Code:
route['default_controller'] = "defaultcontroller";

// Route everything but mentioned controllers to default controller, allowing short urls to work.
$route['^(?!manage[^\w]).*'] = $route['default_controller'] . "/$0";

This way it will select anything with 'manage' in the string - but that is NOT followed by a word character. So manage will match, but management won't.

You'll want to test 'testmanage' to see if that matches. If it does, you'll need the even more specific route:

Code:
$route['^(?![^\w]manage[^\w]).*'] = $route['default_controller'] . "/$0";

Annoyingly specific, but it should work Smile


Routing question - El Forum - 11-20-2009

[eluser]Russ Back[/eluser]
Thanks, you're a star.

I'm working on the following basis but it feels wrong. Your solution looks more elegant.

Code:
$route['^(?!^manage$|^manage\/|^login$|^login/).*'] = $route['default_controller'] . "/$0";