Published by Mijingo

movie icon image

ExpressionEngine How-to Articles

Topics range from beginner to advanced, but are all born out of real world, professional, day-to-day use of ExpressionEngine. Need more information? Get training videos and ebooks on ExpressionEngine from Mijingo.

A Gentle Introduction to CodeIgniter

Editors Note: I’d like to Welcome Kenny Meyers as the newest guest author on EE Insider. Kenny is a web developer at Seattle, WA based Blue Flavor. He loves ExpressionEngine & CodeIgniter for their “less is more” approach and the ability to generate good, clean standards-compliant markup.

So you’ve heard the news that ExpressionEngine 2.0 is built entirely on CodeIgniter and with EE 2.0’s release slowly creeping up on us it’s time to brush up on the framework. The following is a light introduction to the philosophy of CodeIgniter.

Note: All code is written in PHP5 and follows Object-Oriented conventions. If you are not familiar with these conventions, may I suggest:

Before we can get started, we need to download the CodeIgniter framework. There are two ways to get CodeIgniter: downloading a zip package or through a version control system (Subversion). The difference between the two is the difference between having the latest stable and tested build (the zip package) and the latest build (checked out from Subversion).

There are changes made to the subversion repository that won’t show up in the downloaded copy until they are tested. You get access to new features and bug-fixes quickly, but it can also break websites and whatever you’re building, if you update it with Subversion. If you’re using CodeIgniter for a production website, you should always download and use the latest stable release.

Downloading CodeIgniter Stable

We’ll be working off of CI stable for this little project. To do that:

There you go, CodeIgniter’s software is now installed on your system.

Installing Locally

To have CodeIgniter up and running locally, I refer you to EE Insider’s article on MAMP and XAMP. You will need a server running PHP and Apache.

The process is almost precisely the same as installing ExpressionEngine locally except there is no wizard with CodeIgniter. Once your files are in your httpdocs directory, you’ve installed CodeIgniter. This is intentionally basic as CodeIgniter is not meant to tie you to a database or any system except PHP.

You will be working out of the /application/ directory in the /system/ folder of CodeIgniter and like ExpressionEngine, it’s best to rename your system directory or move it below the httpdocs directory. However, because this involves config files, and this article is an introduction, I refer to you to the CodeIgniter documentation.

It should be noted, that there should be no reason for you to add any code or .php files outside the /application/ directory.


MVC is an architecture/organization system for handling what happens when people go to a url on your website. It stands for Model-View-Controller.

Say, for example, that your computer breaks down and you need to get a new hard drive. You make a request to the Apple Store for the item and The Apple Store receives it. To the store, it doesn’t matter whom the request comes from or where it’s from, it just matters how it came to the The Apple Store’s attention and what instructions it has received.

The Apple Store goes and checks with its supplier giving it the instructions it received for parts. The supplier checks his stock returning with the necessary supplies. The supplier then takes those items and ships them to The Apple Store. The Apple Store,  then receives the supplies and contacts the customer. The customer then receives the laptop with the new hard drive and shows it off to his friend.

This is how MVC works.

OK, now for a more practical, less metaphorical take. Let’s take this http request as an example:

  1. The CodeIgniter middleware (the director of every http request) calls out to the PHP class called repair.php in the controllers directory in our application folder.  This is because the first segment of the URI is /repair/. The class looks like this (simplified for obvious reasons):
     class Repair extends Controller {
      public function __construct()
      public function harddrive()
       $stock = $this->Model_harddrive->get_stock();
       $this->load->view('user_view', $stock);
  2. The fine-grained class then looks for the harddrive method in its body because the second segment in the URI is /harddrive/ which in CodeIgniter means “call this method in the repair class”.
  3. When it runs the harddrive method it has to request the harddrive_model Model to get the stock from the mySQL database. So it loads the file /models/model_harddrive.php and runs the get_stock method.

     Class Model_harddrive extends Model {
      public function __construct()
      public function get_stock(){
      $query = $this->db->get('harddrive', 10);
      return $query->result();
  4. The get_stock method in the drug_model, calls up the database and requests the 10 rows from the “harddrive” table, it then returns those results to the controller.
  5. The controller then accepts the stock in a variable called, quite cleverly, $stock. This is then loaded into the view. The view gets loaded with the $stock variable filled in. In this example we just want the number of stock so we can make a point. This will appear as regular HTML.

    <body id="home" class="">
     <div id="wrapper">
      <h1>I've got <?php echo count($stock); ?> new hard 
      drive(s) repaired. Yipee!</h1>

Thus, we have completed the MVC cycle.

This is a very basic example and there is certainly more complexity to the framework then this, however it is the base philosophy behind CodeIgniter applications.

Helpful Resources

CodeIgniter Manual Links:

MVC Links:

CodeIgniter Links:

Posted on Apr 07, 2009

Filed Under: How-To, CodeIgniter, Web Development,

Kenny Meyers
About Kenny Meyers

Kenny Meyers is a web developer based out of San Francisco who currently works for the excellent Kenny has worked on the web in some capacity for 12 years and has helped organize, write and build some great ExpressionEngine sites including and the upcoming Omni Group website. When he's not crying alone, Kenny writes regularly, abusing adjectives and hyperbole, at Happy Magic Fun Time and runs his mouth on twitter @kennymeyers.