CodeIgniter Default Controller Routing

In CodeIgniter you use application/config/routes.php to set up the default controller for routing (the controller that automatically takes over when one isn’t specified) for example:

$route['default_controller'] = "user";

This is a good start, but the URL still needs to specify the controller when accessing functions outside of index. For example the URL needs to be: http://localhost/user/login instead of simply http://localhost/login to get to the user login function.

I haven’t come across an elegant fix for this. Things like using the $route[’(:any)’] parameter tends to break other controllers.

But what you can do is simply create a route for every matching function in your default controller. E.g.:

$route['login'] = 'user/login';
$route['logout'] = 'user/logout';

One caveat is if you have a function that matches another controller name you’ll need to change either the function name or the conflicting controller name.