An Introduction to CodeIgniter


CodeIgniter is really one of my favourite web development frameworks for PHP. It's lightweight, fast, and very easy to quickly get something working in. It follows a model-view-controller (MVC) pattern, and comes with many helpful libraries to help you get started. If you want to try out your first framework, CodeIgniter is the one to start with.

Why CodeIgniter?

The main reason why CodeIgniter is great is the fact that it is extremely lightweight compared to other frameworks. The CodeIgniter framework comes with a very small footprint, and delivers exceptional performance for web applications.

Another reason for choosing CodeIgniter is it is so simple. Even beginners with this framework can get everything they need setup in minutes. You don't need to install anything, or type in any commands, you just upload it to your server.

And finally, according to the CodeIgniter website:

CodeIgniter is an Open Source Web Application Framework that helps you write kick-ass PHP programs.

Let's Get Started!

Like I said before, setup of CodeIgniter is very simple. If you're a beginner with web development frameworks, you should be able to get CodeIgniter all setup in a few minutes. If you're coming from another framework, such as Ruby on Rails, you'll be surprised at how little setup there is to do.

First, you have to download CodeIgniter. Once that is done, extract the folder from the Zip archive. Then upload the contents of that directory to your web server, where your application will be located.

Our setup is almost complete! Let's just take a look at the folder structure of CodeIgniter.

Inside the root of CodeIgniter, you'll see a directory called "system". And inside that you'll find another directory called "application" (we can ignore the other directories for now).

The application directory is where you will be writing the actual code for your application. The structure of this directory looks something like the image to the right.

For now we will only be focusing on the following four directories.

  • config: This directory contains several files for storing configuration settings, such as database information and auto-loaded libraries.
  • controllers: They contain the main scripts that your application runs. Your controllers directory is like the "brain" of your web application.
  • models: This directory contains scripts that handle your site's data. For example, your models may handle database interactions. Information from the database is then passed from the model to a controller.
  • views: The files here control what the user sees, so it is like the site's template files. Views are loaded from controllers, usually with data passed through.

A Bit of Configuration

I know, you're waiting to start writing some actual code. We just have a tiny bit of configuration to do first. Inside the config directory, open the config.php file in your favourite text editor. You need to change your base URL. Somewhere around line 14 should be something like this:

$config['base_url'] = "";

You should change the value to the URL of the root of your application (with a trailing slash). Mine is hosted locally, in a directory called "ci", so it would look something like this:

$config['base_url'] = "http://localhost/ci/";

Now that that is out of the way, we can get to some actual coding!

A Simple Application

To get started with CodeIgniter, we will make a very basic program that displays a message to the user. The purpose of this example is to become familiar with the MVC pattern. (Well, technically VC because we won't worry about models today)

First of all, create a new file in your controllers directory. We'll call this message.php. Inside that file, type in the following code.

// message.php
class Message extends Controller
    function index() {
        $msg1 = 'hello, world';
        $msg2 = 'a second message';
        $data = array(
            'msg1' => $msg1,
            'msg2' => $msg2
        $this->load->view('display', $data);

First off, we start out by defining a new class called "Message". This is the name of our controller. You can see that it extends the Controller class, which includes all the functions that make the controller work.

Inside of our controller, we define a function. Functions, inside of controllers, are like the pages inside of the controller. The function named index() will be the default function that is loaded when a user accesses the controller without specifying a page.

Inside the index function, this is just some normal PHP code. Creating a couple variables, and an array. And then, there's a little special line of code.

$this->load->view('display', $data);

This line of code tells CodeIgniter to load a view called "display", with the data stored in an array ($data) to be passed to the view. We don't have a view called "display" yet, so let's create one!

Inside of the views directory, create a new file called display.php. Type the following code inside the file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "">
<html xmlns="" xml:lang="en">
    <title>CodeIgniter Test</title>
    <h1>CodeIgniter Test</h1>
    <p><strong>Message 1:</strong> <?php echo $msg1; ?></p>
    <p><strong>Message 2:</strong> <?php echo $msg2; ?></p>

So when this view is loaded, the code above is what is displayed to the user. You'll see that we have two variables, called $msg1 and $msg2. Remember the $data variable we passed in to our view? These variables are created from that array, so $msg1 = $data['msg1'] and $msg2 = $data['msg2'].

To test your program, point your web browser to "/index.php/message", after your base url. So for my setup I would go to


If you see a page that displays the data passed to your view, then you can be sure that everything works! Now let's continue a bit more.

Create a new function, or page, inside of your controller. We'll call this one foobar(). Insert the following code.

function foobar() {
    $msg1 = 'foo';
    $msg2 = 'bar';
    $data = array(
        'msg1' => $msg1,
        'msg2' => $msg2
    $this->load->view('display', $data);

You can see that this is exactly the same as index(), except for the values of $msg1 and $msg2. We already have our view setup, so we can test this out right away! Point your browser to index.php/message/foobar/, so I would be going to http://localhost/ci/index.php/message/foobar/.

Now, instead of the previous messages, you should see "foo" and "bar" displayed. This is of course, a very simple example. Just imagine the endless possibilities with this!


CodeIgniter is a great open source web development framework for PHP, using a very powerful MVC pattern. As you have seen in this tutorial, it is very easy to create simple web applications in just a few minutes, using CodeIgniter.

For more information, you can see the CodeIgniter website, which includes the CodeIgniter User Guide, an excellent reference for the many features included in this framework.

Be sure to stay tuned for more advanced CodeIgniter tutorials in the future! To stay updated at FWebDe, you can subscribe to the RSS feed, or subscribe to get updates via Email.

The code for this tutorial is also available to be downloaded:

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!


  1. March 29, 2010

    I've been meaning to try out CodeIgniter, so just ran through your tutorial and it worked great, was a nice and simple introduction! I'd love to see more in the series as well! :]

    • March 29, 2010

      Thanks for the comment! And I do plan on writing more CodeIgniter tutorials in the future!

  2. Carlos
    March 30, 2010


    I'd love to see more Codeigniter tutorials too.


  3. John
    April 14, 2010

    Not available for linux :(

    • April 14, 2010

      Where does it say that? I use Linux and it all works perfectly fine for me.

  4. December 7, 2010

    Hi thanks for great tutorial.
    i would like to see more on this ..

    keep it up..

  5. Sam
    February 22, 2011

    If this doesn't work for you at first...try adding your parent fold name followed by an underscore to the controller extension i.e.

    class Message extends CI_Controller

    • February 24, 2011

      Its for work codeigniter2 version only

      • Keeny
        November 28, 2011

        Thanks Sam & fareed.

        If your using CodeIgniter 2+, change the class to:
        "class Message extends CI_Controller"

        (nothing to do with the parent fold, the main controller was just renamed 'CI_Controller' I guess)

        great tutorial

  6. February 24, 2011

    Great way of starting a tutorial for beginners .. I appreciate it .. thanks a lot

  7. Lefrandi
    October 20, 2011

    Thank you very much friend!
    This is very simple, i like it and
    i 'm understand..
    Keep writing codeigniter :)

  8. November 24, 2011

    great tutorial for beginners.

    can u tell me in the controller instead of the index() we can use other function. if i am using other function then it is showing error
    the url is http://localhost/codeigniter/index.php/message.
    and http://localhost/codeigniter/index.php/message/foobar/ this is fine.