2009
20
Oct
Category: CodeIgniter, PHP

I was playing with CodeIgniter routing at work. I wanted to have a catch-all route but I needed to exclude a certain controller, or controllers, from that catch-all route. As an example, I wanted to have http://www.example.com/(ANYTHING) route to the “page” controller. http://www.example.com/policies/shipping would route to the “policies” controller and then to the “ship” method. From there the “page” controller would split up all of the segments.

I started out with this:

$route['policies/shipping'] = "policies/ship";
$route['(:any)'] = "page";

The “policies/ship” route would be caught by the “policies” controller and everything else would be caught by the “page” controller. Except the “page” controller was getting everything.

I needed to exclude certain controllers from the catch-all route. After a Google search, I came up with nothing. So I started working on it myself. Here is what I cam up with:

$route['policies/shipping'] = "policies/ship";
$route['^(?!policies|controllerA|controllerB)\S*'] = "page";

So far it seems to work perfectly. Hopefully this helps someone else out.

There is 1 comment so far

1
November 14, 2009 @ 5:50 pm

Hi David! Thanks for this, I just made use of it and it worked very nicely for me. Might eventually make the “controllers” part of it automated based on a scan of the controllers directory, but for now this is perfect.

Add a Comment

Allowed tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">