Brain’s routing is similar to CodeIgniter’s in that the first URI segment is the controller, any second URI segment is a method other than index, and any additional URI segments are method arguments. Routes can also be configured in config/routes.php to swap route for route. The default route is also defined in config/routes.php.


Once the router class has determined which controller and method to call, it passes that information to the load_controller method. This method calls the requested controller and method. The beauty here is that after a controller and method are called, the load_controller method can be called again if necessary. So for instance, if you have a situation where you might do an internal redirect, you could instead just use load_controller to load up the controller and method you wish. This is really nice if there is session, cookie, or post data, because there is no need to recycle the data.

Dirs for Controllers

As configured in config/core.php, Brain will first attempt to find your controller in APPPATH . ‘controllers/’, and if the controller isn’t found, it will look in BRAIN . ‘controllers/’. You can add more dirs for controllers if you wish, simply use the add_path method, and supply a full path to the location.

Routing Config

Besides standard text matching for custom routes, Brain offers full regex matching of routes. Mod rewrite may be preferred for complex URL rewriting, but if the key of your routing rule is regex, the value associated with the key is used directly. In most cases you may want to route to a method in the controller where the URI segments can be analyzed, then call another method of further action.