I want all uri of my site route to one controller. Except The uri starting with home, admin, login or sigun_up will not route to that one controller. How would you do that?
I'm thinking some kind of reg ex, something like this
$route["all link route to this controller, except uri starting with admin, login, home and sign_up"] = 'boss controller';
I think you cannot do negative routing.
But you can do this following way:
$route['(admin|login|home|sign_up)'] = "$1";
$route['(admin|login|home|sign_up)/(:any)'] = "$1/$2";
$route['(:any)'] = "boss_controller";
According to the codeigniter documentation you can use regex within the routes files, so in this particular case, pulling from another SO Answer by knittl:
\b word boundary
\< word start
\> word end
We can use the word boundaries to set your words to ignore and then do an opposite regex for the other case.
Regex to grab all but admin, login, home, signup:
([^\<(admin|login|home|sign_up)\>+-])/g
Regex to grab specifically admin, login, home, and signup:
([\<(admin|login|home|sign_up)\>+-])
Note the use of the ^
character to denote the inverse relationship.
So the final route should look something like this:
//Typical Route
$route['products/([a-z]+)/(\d+)'] = "$1/id_$2";
//Custom Route to go TO the inverse
$route["base/([^\\<(admin|login|home|sign_up)\\>+-])/"] = "base/$1/";
//Custom Route to go TO admin/login/home/sign_up
$route["base/([\\<(admin|login|home|sign_up)\\>+-])/"] = "base/$1/";
Haven't tested it, but in theory it should work.
I'm not familiar with CodeIgniter but since CI supports regex in routering, you may use the regex ^\/(?!(admin\b)|login\b\/*|home\b\/*|sign_up\b\/*)[a-zA-Z0-9_\/]*/g
to grab anything but /admin /login /home or /sign_up. Here's a demo you may have a look.
Explain:
^\/
matches the starting /
in the URI
(?!(admin\b)|login\b\/*|home\b\/*|sign_up\b\/*)
is everything but not admin|login|home|sign_up, here \b
"assert position at a word boundary (^\w|\w$|\W\w|\w\W)" which means you can use something like adminabc or loginxxx if you want, there will be matched correctly.
[a-zA-Z0-9_\/]
this part is acceptable characters in your URI.
Result:
------Not passing-------
/admin
/login
/admin/xxx
/login/xxx
/home/xxx/login/xxx
/sign_up/xxx/login/xxx
--------passing---------
/
/admintest/xxx
/loginetest/xxx
/hometset/xxx
/sign_uptest/xxx
/others
/others/xxx/login/xxx
/others/xxx/others/xxx/ooo
Hope it helps.