CodeIgniter (A framework with Model View Controller)

CodeIgniter is a PHP framework that helps structure your code and make redundant tasks less tedious. There are countless similar PHP frameworks, the most popular ones being CakePHP and symfony. After looking at several frameworks, the reason I chose to work with CodeIgniter first is that it is a more loosely formed framework, serving as a middle ground between convention and convenience. A trend of framework these days, such as Rails and CakePHP, is to favor convention, forcing strict rules. I won’t argue the benefits/negatives here but I find both claims reasonable. CodeIgniter doesn’t impose those rules and gives you more freedom to write your code and use the framework whenever you want to. Therefore, it’s a good transition between no framework and a full-fledged framework. Also, it is possibly one of the best documented frameworks with tutorials and great documentation of the classes.

A theme to all frameworks nowadays is the Model-View-Controller (MVC) architecture. So let’s hammer this down to the ground before starting. A common philosophy to programming is separating the logic and the presentation. This is the heart of the MVC architecture. Let’s examine the easiest part to understand first.

  • View
    • This is the presentation to the user. A way to think of this is let’s say you’re given a Word document with all the information that your client wants for a website. You don’t have to do any server end scripting. Basically, you just have to design around the given information and then plug it in. This process is what the View does: takes the finalized information and sticks it into a pretty design to display. What should come to mind are client side presentation technologies such as HTML, CSS, and JavaScript. There can be PHP code but theoretically there should be as little logic as possible. You’ll want to use some logic for looping through arrays and making minor decisions. You hear View -> Think template, HTML, pretty.

Now for the two that I find slightly harder to distinguish: Model and Controller.

Both are part of the logic portion of the model and do the “thinking” of the process. This includes retrieving information from databases, processing headers, sorting data, authentication, security, and so on. The “brains” behind the website.

  • Model
    • Models can be thought of as data structures, kind of like classes in object-oriented programming. They interact closely with the database and XML data. You want to get the last ten posts from the database? Ask a model to do that for you. They do the brute work: working with the database (getting, updating, inserting, deleting information).
  • Controller
    • Last but definitely not least, the controller. The controller will interact with the user and process all the requests. The GET and POST headers will go through a controller. The controller is the center of the application and serves as a channel that can request information from a model and throw it out to the view or take some header data and ask a model to update the database. It serves as the information desk and the mediator for the Models and Views. The central brain of the system.

These properties lend to a common flow through these structures. The front user sends a request which is taken by a controller which decides what to do. The controller will do whatever calculations it needs and will ask for information from the models and/or send information to the models. After all the logic is done, the controller has the final dynamic information that will be presented to the user. This information is sent to the view where it is plugged into the template and presented to the user. The end.

This style of coding is in almost all frameworks but let’s examine this in terms of CodeIgniter now.

Some frameworks implement MVC by imposing rules, such as what database tables need to be called, something being the plural of another and so on. CodeIgniter is pretty slack on this and lets you name your classes whatever you want. There are a couple restrictions that apply and the ones worth mentioning are:

  • controller and model class names must be capitalized
  • controller and model file names have to be the same as the respective file names but lower case

When a user accesses a CodeIgniter application, the URL call is like the following: index.php/controller/action

This will access the controller with the same class name and then execute the specified action (or index if not given). Then your controller will call models and do business logic as needed.

While others enforce frameworks enforce the use of all three components and naming conventions, CodeIgniter does not enforce the use of models and in fact, the name of all three components can be completely unrelated.

I hope this clarifies some confusion on the MVC approach. Check out how to easily implement this in the great CodeIgniter documentation.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: