CodeIgniter: Keeping M-V-C straight

So I took the plunge and started taking frameworks a little more seriously.  This is due in part to a presentation I attended during Codeworks Philadelphia.  Cal Evans gave a presentation about Silex, and in that presentation made a statement to the effect of “Developers should know 2 or 3 frameworks”.  If I remember correctly, the gist of this statement was based around the idea of selecting the right tool for the job, at least that is how I remember it.

Anyway, I decided to pick CodeIgniter back up and start really trying to learn the ins and outs of it.  I was pleasantly surprised that overall, it is pretty simple to use and whenever you want to do something, the first question to ask yourself should be “Let me check the user guide and see if there is a library or helper for this before I write it”.   Chances are, the answer has already been thought of and exists in the framework.

The one area where I see a lot of confusion for people who are picking up CodeIgniter for the first time is sorting out what belongs where using the MVC pattern.  The CI manual does say that you are not required to follow a strict MVC pattern, but from what I have seen, putting business logic and data retrieval into the controller methods gets very messy in a hurry, and it does not lend itself to future expansion, testing, etc.  So let’s cover the MVC pattern as implemented by CodeIgniter and try to sort out what goes where:

Models:  This is where the work should get done.  All of your methods to make calculations, get records, update & insert, etc. should be encapsulated in a model, preferably using one class for each “object” such as a user, a sales order, a page, etc.  Every class that is categorized as a model should be stored in the application/models folder.   So the basic thing to remember is that if you are manipulating information or interacting with the database, do it in a model.

Views: This is what gets displayed on the screen.  Your HTML code should be here, and your CSS will affect your views.  In most cases, if you are following MVC strictly, then chances are the only PHP code in the file will be echoes of variables, & possibly some loops.  Alternatively, you can use a template parser and have basically no PHP code in the views at all.  The bottom line:  you should not be manipulating or changing things in a view.   Whatever data was built in your model should simply be being displayed.  These files are saved in your application/views folder.

Controllers: These are the trucks and roads that move data from your models to your views.  The controllers in CodeIgniter are responsible for receiving a command and then deciding what the application should do in response.  So if there is a command for the application to display the “about_us” page, the controller will receive that command (calling the about_us method in the controller), retrieve any required data for the command, then call up the view and pass the data to it for display.  These files should be located in the application/controllers folder.

That is the basic gist of how the MVC pattern works for CodeIgniter.   Even though the manual says you do not have to strictly follow this pattern (e.g. you can skip models), I would recommend that your follow the design as intended.  It leads to cleaner code which is easier to maintain and work with in the future.



, ,

Comments are closed.