A disturbance of mine in CodeIgniter is when I want a controller named the same as a model. The standard way of solving this is calling your models for example UsersModel, Users_m, or the likes. This has the unpleasant side effect of making your code look fugly. The other option would be to do the same to the application controllers, but this would affect the URL instead of the code which is probably worse in the end since that would affect the end-user. So what to do?
My solution is to add the suffix, but still make sure that the URL example.com/users still gets handled by the Users_Controller. If one googles for this, one of the results is a Nettuts article that describes how to hack the core files to implement this functionality. This is usually a big no-no for me, so I’ve chosen to go another route, namely routes (yes - pun intended).
First of all, let’s be honest: This is not perfect. At all. It has one major flaw, and that is it can’t handle controllers stored in subfolders. So, if you for example would like to keep a number of controllers regarding the administrative part of your site in the controllers/admin folder, this wouldn’t work. This is because all it does is check for the first slash in the URL and that which comes before must be the name of the controller or we are screwed.
However, it has worked for me on occasion and it does get you out of messing with the core, so without further ado I present to you: the code.
1 2 3 4 5 6 7 |
|
Yup, that’s it. That goes in your application/config/routes.php file. I included the default_controller key so you won’t forget to add the suffix in the destination controller. I hope this helps, but remember that if you go ahead and break your client’s website with this code, it’s all on you!