Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter route regex - match any string except 'admin'

I'd like to send any route that doesn't match an admin route, to my "event" controller. This seems to be a fairly common requirement and a cursory search throws up all sorts of similar questions.

The solution, as I understand, seems to be using a negative lookahead in the regex. So my attempt looks like this:

$route['(?!admin).*'] = "event";

..which works. Well, sort of. It does send any non-admin request to my "event" controller, but I need it to pass the actual string that was matched: so /my-new-event/ is routed to /event/my-new-event/

I tried:

$route['(?!admin).*'] = "event/$0";
$route['(?!admin).*'] = "event/$1";
$route['(?!admin)(.*)'] = "event/$0";
$route['(?!admin)(.*)'] = "event/$1";

... and a few other increasingly random and desperate permutations. All result in a 404 page.

What's the correct syntax for passing the matched string to the controller?

Thanks :)

like image 249
Wintermute Avatar asked Jan 18 '23 09:01

Wintermute


1 Answers

I don't think you can do "negative routing".

But as routes do have an order : "routes will run in the order they are defined. Higher routes will always take precedence over lower ones." I would do my admin one first then anything else.

If I suppose your admin path is looking like "/admin/..." I would suggest :

$route['admin/(:any)'] = "admincontroller/$1";
$route['(:any)'] = "event/$1";
like image 127
Stéphane Bourzeix Avatar answered Feb 13 '23 12:02

Stéphane Bourzeix