What’s in a name?
Like I would imagine many programmers, I have a tendency to get a little anal about naming my Controllers and Models. Combine this with the default behaviour of CodeIgniter to parse the URL segments into controller/function/id parts, and it all begins to get very messy unless some careful thought is put into this stage of a project.
For my finance application there are a few obvious ‘pages’ needed. A user will want to view their list of accounts, or an individual account. Using the default structure, a sensible URL design might be:
- /accounts to list all accounts
- /account/details/12345678 to view the transactions of a particular account
The latter URL would require a Controller named ‘Account’ containing a method named ‘Details’. That all sounds okay so far, but what happens when you throw Models and ORM into the mix?
I plan to use a hand-rolled ORM solution to handle the database interaction within the app, which would mean for the ‘accounts’ table, my Model class would be named… ‘Account’. And you can’t instantiate two separate classes with the same name. Problem.
Assuming (and sticking with convention over configuration) that the Models have to be named correctly (’Account’ for the accounts, ‘User’ for the users, and so on), how should I decide on the naming of my Controllers?
As I see it, there are two options:
- Pick a convention for Controllers and stick to it. For example: account_controller, user_controller - I can even use the URL routing feature to redirect my original choice for URLs to the newly named controllers, although in my experience relying on routing can start to get messy.
- Come up with a new design for my URLs and reflect that in my Controller names. This could be anything - /show/me/12345678, /list/transactions/from/12345678 - the possibilities are endless. This option has the benefit of avoiding complicated routing rules, but could prove less intutive to write and maintain.
I’m leaning toward Option 2, based on a combination of disliking excessive routing and loving beautiful URL design. I’d love to hear what conclusions other MVC application developers have come to, though.


Leave a Reply