Welcome Guest, Not a member yet? Register   Sign In
Routing question
#1

[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.
#2

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

[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
#4

[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.
#5

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

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

[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
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB