"RT @richardosman: I bet people who narrate audiobooks always have their voices recognised the second they start speaking at 1.25 speed."

@ashleyford 5 days ago

"All the high value adverts on display tonight! #itv #xfactorfinal #xfactor"

@ashleyford 1 week ago

Designer and web developer, Co-founder and Technical Director at Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]

Get started with CodeIgniter


I’ve never really got the hang of PHP frameworks and have tended to use my own custom built CMS but even with this I still find myself writing the same code over and over again. In the past I’ve dabbled with CakePHP and the Zend Framework and not really got very far, so a few days ago I downloaded CodeIgniter. Although I’m extremely new to this I was pretty blown away by the simplicity of setting things up and getting an actual webpage that processes something online.

How It Works

CodeIgniter uses the MVC or Model View Controller architectural pattern, if your not familiar with MVC it is a logical object orientated development approach. below we look at a simple example of how we use the MVC.



A controller is simply a class file that is named in a way that can be associated with a URI. For example if i have a controller with the filename account.php the URI to reach that page would be something like so you no longer need to fiddle with the .htaccess file and mod_rewrite to enable SEO friendly URI’s. The controller is the top level file for each page that allows you to include database requests in the form of ‘Models’ and templates as ‘Views’. below is an example of a controller, This code would be saved in the file blog.php, it’s important to understand the naming convensions because the name of the file is always the name of the class with the first letter capitalised. Within the Blog class we have an index() function which is always loaded when the page is excecuted. For example if we had another function lets say ‘categories’ the code within that function would only be executed if we visited this is also the way you can pass url parameters into functions but we’ll leave that for another day. We’re loading our ‘View’ in at the bottom and passing the array $data into it. Within the view file which would be called blogview.php if we wanted to echo the heading we’d simply echo $heading the same with the title. With the to do list we would need to setup a for each loop to cycle through the results in that array.

class Blog extends Controller {

	function index()
		$data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands');
		$data['title'] = 'My Real Title';
		$data['heading'] = 'My Real Heading';

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


As yet we’re not using a model, this is because our controller isn’t doing anything complex. If we wanted to connect to a database and get a set of results we would use a model which could look like the below. This is all the code we need to pull 10 results from the database which would be returned to the controller in an array. note that you would need to replace ‘tableName’ with your own database table name.

class Blogmodel extends Model {

    function Blogmodel()
        // Call the Model constructor

    function get_last_ten_entries()
        $query = $this->db->get('tableName', 10);
        return $query->result();


To include the model firstly you need to navigate to system/application/config/database.php and add your database connection details. then below you’ll see that i’ve modified the controller that we used earlier so it now connects to the database and gets our data. This will then be sent through to the ‘view’.

class Blog extends Controller {

	function index()

		$data['title'] = 'My Blog Site';
		$data['heading'] = '10 of my blogs';

                 // connect to DB and get data
		$data['query'] = $this->blogmodel->get_last_ten_entries();

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


Simply put a ‘View’ is our template, it’s where our data gets rendered and is made pretty, you can have as many views as you like and pass whatever data you like into them it’s probably a good idea to have a header, main content area and footer, but for the purposes of this demo i’ve used one view where all the data is rendered.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title;?></title>


<h1><?php echo $heading;?></h1>

<?php foreach($query as $item):?>

<h2><?php echo $item->title;?></h2>
<?php echo $item->content;?>

<?php endforeach;?>

For more information on where you can find the download and user guide Click Here.

Designer and web developer, Co-founder and Technical Director at Previously I worked at Spotify, MySpace and InMobi. Contact me - ashley[at]

Comments 7
  • anis ur rehman
    Posted on

    anis ur rehman anis ur rehman

    Reply Author

    thx. i am new user of this and this intro is realy helped me to go further

  • Daniel
    Posted on

    Daniel Daniel

    Reply Author

    I have used both CI and CakePHP. After developing for a long time and for a living. You can develop much more robust applications much faster using CakePHP. It does have a steeper learning curve than CI, but the Convention over Configuration paradigm is key. CakePHP promotes code reuse a lot more than CI. I find myself writing much less code using CakePHP. The ORM is also awesome compared to CI’s Active Record implementation. Cake also has a Auth component and an Access Control List component witch CI is lacking. If you are new to PHP frameworks CI is the way to go. Once you are comfortable in a framework like CI you will find that it is lacking compared to CakePHP.

  • Jeff
    Posted on

    Jeff Jeff

    Reply Author

    Nice overview, thanks! I know that I have a friend that is big into PHP framework usage and he used to use CI but has since migrated to Kohana (, saying that while based on CI, it is MUCH better and even faster.

    Thanks again!


  • Martin
    Posted on

    Martin Martin

    Reply Author

    Nice intro to codeigniter, I’ve been waiting for the right project to try out CI, looks promising.