Welcome Guest, Not a member yet? Register   Sign In
Routes - Everything BUT X to default controller
#1

[eluser]Yahtzie[/eluser]
I am developing a fairly large site which has quite a few different admin sections (all with different access ... but that's a different story). I recently struggled to configure a route to route everything EXCEPT my admin pages through the default controller. I tried using:

$route['web_admin'] = "web_admin";
$route['store_admin'] = "store_admin";
$route['blog_admin'] = "blog_admin";
$route[':any'] = "default_controller";

but this just routed everything (including all my admin pages) through the default controller.

After struggling many hours to write a regular expression to do this, I FINALLY figured it out, so I thought I'd share it in case anyone else ever wants to do this:

$route['(((?:.+)_(?!admin).+)|(?:\b[^_]+\b)|(?:\b[^_]*_\b)|(?:\b_[^_]*\b))'] = "default_controller";

Now, I'm sure not all your pages will have the format "something_admin", so here is a breakdown of the statement:

(
((?:.+)_(?!admin).+) <- any string of the format [anything]_[NOT "admin"][anything]
|(?:\b[^_]+\b) <- OR any string that doesn't contain an underscore
|(?:\b[^_]*_\b) <- OR any string that just has an underscore at the end
|(?:\b_[^_]*\b) <- OR any string that just has an underscore at the beginning
)

Hopefully this saves someone else a couple hours of headache.
#2

[eluser]xwero[/eluser]
Code:
$route['([web|store|blog]_admin)(.*)'] = '$1$2'; // should fetch all admin urls
$route['(.+)'] = '$1'; // everything else
I got perl nightmares in the daytime looking at your regex Wink
#3

[eluser]Yahtzie[/eluser]
Hmm... guess that is simpler. I just figured since it didn't work when I manually specified the _admin calls, it wouldn't work to regex them manually either.

This works as well (so I don't have to manually specify each admin site):

Code:
$route['(.*_admin\b)'] = '$1'; // fetch all admin urls
$route['(.+)'] = 'default_controller'; // everything else

Thanks for the quick reply!
#4

[eluser]Yahtzie[/eluser]
On another test, that doesn't actually work. It was actually reading from the wrong spot. Adding
Code:
$route['(.+)'] = 'default_controller';

seems to route everything through the default controller, no matter what other routes are declared. Am I missing something?
#5

[eluser]xwero[/eluser]
The routes are matched from from top to bottom so if you put that route first it will fetch all routes yes.
#6

[eluser]Yahtzie[/eluser]
Never mind ... I figured out why it wouldn't work. I put
Code:
$route['(.*_admin\b)'] = '$1';

instead of
Code:
$route['(.*_admin\b)(.*)'] = '$1$2';

which routed ONLY the base url through the controller. My bad. :S
#7

[eluser]●●●●●●[/eluser]
Can u pls tell me wt dis $1,$1&$2 etc mean..
#8

[eluser]TheFuzzy0ne[/eluser]
Regular Expressions capture the matched strings between the brackets. You can access the captured text with $1, $2,$3 etc...




Theme © iAndrew 2016 - Forum software by © MyBB