What is CodeIgniter and How Does it Work


This article explains what the CodeIgniter framework is and gives insight as to how CodeIgniter works. 

CodeIgniter is Object Oriented, Model-View-Controller, a Rapid Application Development framework for PHP, that has a small footprint, is blazing fast, has a short learning curve, and is easy to install. CodeIgniter does this by providing commonly needed functionality, helpers, and libraries.

What is CodeIgniter

CodeIgniter was created by Rick Ellis the owner of Ellis Labs. The first release was on February 28, 2006. CodeIgniter was wildly successful and very popular within the PHP community. 

CodeIgniter has a lot going for itself.

Small Footprint

Version 3.1.11 is only 16 megabytes in size. That is small. In the early days, CodeIgniter was less than 4 megabytes.

Version 2.2.6 is about 4K compressed and is 5.1 megabytes uncompressed.

Blazing Fast

Most report CodeIgniter is lightning fast. It is compatible with shared hosting which it’s pears cannot say. The CodeIgniter framework puts little stress on the hosting server.

Excellent Documentation

The manual covers, in detail, every aspect of how to use CodeIgniter to build websites and web applications. All features and functionality are well documented.

The documentation can be viewed online at: Codeigniter 3 User Guide.

It is worth noting that in the upper right corner of the page there are three lines next to the search box. Clicking the three lines changes the layout to the old-style layout which is much more helpful. 

CodeIgniter Manual Layout Toggle

Once clicked, a tab will appear in the upper right area named “Table of Contents”. If clicked, it will display a full menu that will give you access to every bit of the documentation.

CodeIgniter Manual Menu Drop down Image

Easy to Use

Ease of use is one of CodeIgniter’s hallmarks. It was created early before PHP added several features that are complicated. CodeIgniter 3 is simpler than CodeIgniter 4. CodeIgniter 4 embraces the PHP features that make it more complicated. For the one-man-band, those features are not necessary.

Easy to Install

To install CodeIgniter all that is required is to download the zip file and upload it to your server and unzip it. And point your web browser to the instilled Codeigniter and it should show you the welcome page.

Object-Oriented

Even though CodeIgniter is Object-Oriented, an inexperienced programmer will probably not notice since everything is well structured and well documented.

Model-View-Controller

As you probably already know CodeIgniter is Model-View-Controller. This design pattern is widely used and respected. It helps structure one’s code and makes it easier for multiple developers to work on the same project. 

What is a Controller – This is the boss of the other two. It is the brain. This is where most of the programming logic is placed. 

What is a View – Is the display layer. This is where the HTML, Cascading Style Sheets (CSS), JavaScript, AJAX, and content come together to create what shows in your browser. 

What is a Model – This is where all the database functionality lives. These functions are called by the controller so the data can be processed and then sent to the view.

License

The current owner, British Columbia Institute of Technology, uses the MIT License. Here is what it says:

Copyright (c) 2014 – 2019, British Columbia Institute of Technology

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The Online License

Versions

Currently, there is two versions of CodeIgniter. Version 3 and 4.

Some of the differences are:

  • CodeIgniter 4 is not backward compatible.
  • Version 4 has added support for Namespaces.
  • Autoloading support has been added to version 4

Life Cycle

According to the Codeigniter forums, Codeigniter 3 will be supported for about 2 more years. 

The repository will live on. The only real issue with using Codeigniter 3 after community support ends is PHP version upgrades and security issues will go unaddressed.

Loosely Coupled

CodeIgniter is referred to as loosely coupled because only a controller is required. The model and view are not required to make a working website. Though, this is not the default configuration. 

CodeIgniter Release History

VERSIONRELEASE DATEEND of LIFE
1February 28, 2006Unknown End of Life
2January 2011October 2015
3October 2015Currently Supported
4February 24, 2020Currently Supported

Helpers

There is about 21 helpers. Each is a set of functions that can be plugged into your code as you build your application. Helpers help the developer accomplish tasks. To get a feel for what a helper is let us look at a couple of helpers that are routinely used.

URL Helper Example

The URL helper is a set of functions that make working with URLs easier. You can read the on-line URL Helper documentation for more information.

For example, to convert the title of an article into a URL string, the helper function url_title($title) could be used. Here is some sample code:

$title = "Why I Like Ice Cream";
$url_title = url_title($title) 

$url_title will contain the string “why-i-like-ice-cream”. Which can be used to create a complete URL like example.com/why-i-like-ice-cream.

The whole idea is the helpers help the developer to write less code and spend less time doing so. This is the rapid application development feature mentioned elsewhere. 

Email Helper Example

The email helper makes it easier for the developer to add email functionality to his or her application.

For example, to validate an email the developer can use the valid_email() helper function.

Here is an example:

$is_email_valid = false;
if (valid_email('someemailaddress@example.com'))
{
        $is_email_valid = true;
}

You can read more about the email helper by reading the on-line Email Helper documentation.

[] 

Libraries

There are about 30 libraries in CodeIgniter 3. Libraries help the developer complete larger tasks such as sending an email. Sending email takes a lot more effort than checking the validity of an email address such as what we previously performed.

The email library is a class that provides comprehensive functionality for sending emails. A glance at the on-line Email Class (library) might be surprising given all the options and setting that are needed to send an email. 

What is CodeIgniter Used for?

Codeigniter is a PHP framework that was created as a PHP rapid application development tool. In the early days, Codeigniter was used to create all sorts of websites. CodeIgniter is still capable of creating any type of website you can think of, however, today it is mostly used to create web applications. 

The Installation Process

If you are familiar with the Linux utility wget you can download the zipped file directly to your server and unzip it.

It is that simple.

It is not necessary to have access to the server command line. 

Installation only takes a few minutes and is just a few steps.

With wget

  1. Use wget to download the CodeIgniter zip file into the hosting root directory.
  2. Unzip the source file.
  3. You may need to rename or remove the leading CodeIgniter directory.
  4. Point your browser to your CodeIgniter install and you should see the welcome page.

Without wget

  1. Download the CodeIgniter source file onto your local computer.
  2. Unzip the source file locally.
  3. Upload the files to your server using FTP. Make sure not to include the CodeIgniter leading or root directory when uploading.
  4. Point your browser to your CodeIgniter install and you should see the welcome page.

Verify it is Working

To verify your installation is working, all you need to do is point your browser to the server where you installed CodeIgniter. 

If it is installed correctly you should see the welcome page.

Image of the Codeigniter Default Welcome Page

This is a page that comes with CodeIgniter. It gives you the path to the view and the controller. Notice the last line contains a link to the User Guide. This is a local copy. You may want to bookmark this link so you will always have access to the documentation off-line.

How Does Codeigniter work

As you have read in this article, CodeIgniter is easy to install, is blazing fast, easy to learn, is well documented, and has a short learning curve. Add to that it is Model-View-Controller.

Codeigniter is a rapid application development tool for building PHP web applications. These applications can take the form of a content management system (CMS) to an interactive web application.

Because CodeIgniter comes with so many of the needed components for building web applications, in so many ways, it is plug and play. This keeps the developer from having to recreate the wheel every time they want to build a web application.

Let start by looking at the file system. Once installed you should see a file system that looks like this:

  • /application
  • /composer.json
  • /contributing.md
  • /index.php
  • /license.txt
  • /readme.rst
  • /system
  • /user_guide

To learn CodeIgniter it would be worthwhile looking through all the directories and open the files and read through them. Do not worry if some of it or even a large portion is hard to understand. It will be a worthwhile excise that will help you as you become a CodeIgniter developer. 

In this article, we will limit our discussion to 4 directories and how they work. Knowing what we will be covering will place you well on your way to becoming a CodeIgniter developer.

As a starting point, let’s look at the welcome controller, welcome view, and the routes configuration that ships with CodeIgniter 3.

  • The welcome controller is located in: application/controllers/Welcome.php
  • You can find the view at: application/views/welcome_message.php
  • And the routes files is located at application/config/routes.php

The default URL mapping is example.com/controller/function/argument. In the case of the URL for the welcome message, it is example.com/index.php/welcome/index. 

Controller

If we look in the controller we see several lines:

1) defined(‘BASEPATH’) OR exit(‘No direct script access allowed’); – this line verifies CodeIgniter is running. If not it sends a message to the browser and stops processing. This is a security measure so the only entry point is through the index.php file located in the root directory. This will keep the bad hackers from accessing your code in ways it was not intended.

2) class Welcome extends CI_Controller – Notice the controller class name is Welcome and it extends CI_Controller. This means it inherits everything in the CI_Controller class.

3) public function index()

{
   $this->load->view('welcome_message');
}

The index function is the default function in the controller. Remember the URL mapping is /controller/function/argument. If no function is listed, the default is the function named index.

In the case of the welcome URL it is /index.php/welcome/. You can drop welcome from the URL leaving it as example.com/index.php.

Two things are taking place. One is the default controller that is defined in the routes.php file is being processed and the default function in the controller is used to complete the process so the welcome page can be displayed.

The line $this->load->view(‘welcome_message’); Tells CodeIgniter to load the view file named welcome_message’) and display it.

View

The view can be located at: application/views/welcome_message.php. A quick look inside shows it contains HTML, Cascading Style Sheets, and a minor amount of PHP code. 

Data can be transferred to the view file and the view file can use PHP to display this data. It is possible to process data in a view, however, all the processing should take place in the controller and model.

Basically, a view is web content or what is known as the visual layer.

Model

In this case, there is no database in use so there is no model. We will cover the model later in the article.

Router

The routes file is located at: /application/config/routes.php. There is only one line in the file that we need to evaluate here. That line is: $route[‘default_controller’] = ‘welcome’;

What this line tells CodeIgniters is the default controller is the welcome controller that is listed above. That is why you can go to the website URL such as example.com/index.php and see the welcome message. 

Removing index.php from the URL

To remove the index.php from the URL (example.com/index.php) refer to the manual page at: CodeIgniter URLs and then scroll down to “Removing the index.php file”.

If you are using shared hosting it is probably already configured to remove the index.php from the URL. If it is not configured you will get a blank screen or an error message.

If an error occurs or you get a blank screen contact your hosting provider for help. 

Create a Model for the Welcome Application

Create a model class named “people_model” in the application/models/ directory. Name the file People_model.php. It is a good idea to create a model for each table. This will keep the models organized and by naming the model the same name as the table it makes it easy to understand what goes with what model and it also becomes self-documenting. 

The model code looks like this.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class People_model extends CI_Model {

	public function get_people()
	{
 		$query = $this→db→get('people');
		return $query->result();
 	}
}

For more information you can refer to the online Model documentation.

We will be using MySql as the data engine. You should have MySql or one of it’s derivatives in your hosting account. 

We need to create a database, a table, and populate it with data.

To do so we need to:

  1. Create a database user. 
  2. Create a database.
  3. Create a table.
  4. Populate the database.
  5. Configure CodeIgniter so it will know about the database and be able to use it. 
  6. Create the model (listed above).

I’m going to make a simple database with a table that contains three names.

  1. Tom Cruise
  2. Tom Selleck
  3. Matt Damon

The SQL for Creating the Database

CREATE DATABASE contacts

The SQL That Creates the Table

CREATE TABLE `people` (
  `id` int(11) NOT NULL,
  `FirstName` varchar(100) NOT NULL,
  `LastName` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

The SQL That Inserts (adds) the Data to the Table

INSERT INTO `people` (`id`, `FirstName`, `LastName`) VALUES
(1, 'Tom', 'Cruise'),
(2, 'Tom', 'Selleck'),
(3, 'Matt', 'Damon');

Configure the Codeigniter Database 

Edit application/config/database.php

Scroll down until you see the following 4 lines

'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',

Set these values according to your database configuration.

For more information you can refer to the CodeIgniter configuration documentation.

Modify the Welcome Controller

In the welcome controller I added a constructor like this :

public function __construct()
{
 	parent::__construct();
 
 	$this->load->database();
 
}

The constructor was necessary so the database could be initialized.

Then I added two lines to the index function:

$this→load→model('People_model');
$data['people'] = $this->People_model->get_people();

and modified the load->view line by adding $data.

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

This transfers the needed data to the view.

Modify the Welcome View

/application/views/welcome_message.php

Immediately after the <div id=”body”> tag, add:

<ul>
<?php
foreach ($people as $arr) {
	echo '<li>';
	echo $arr->id.' ';
	echo $arr->FirstName.' ';
	echo $arr->LastName.' ';
	echo '</li>';
}
?>
</ul> 

The Output

Image of the CodeIgniter Modified Default Welcome Page

As you can see the three names are listed on the home page.

Everything in CodeIgniter 3 is that easy. 

Related Questions

Is CodeIgniter easy to learn?

CodeIgniter is very easy to learn. If you already know how to program in PHP at the beginner level and understand the basics of Object-Oriented Programming in PHP, then you will find CodeIgniter easy to learn. CodeIgniter has the shortest learning curve of all the PHP Frameworks.

How long does it take to learn CodeIgniter?

To learn CodeIgniter might take 10 to 15 days if you spend 2 hours a day studying CodeIgniter. Hands-on is very important. If you follow the brief tutorial listed above, you will be well on your way. Learning Codeigniter comes down to understanding how to create models, views, controllers, and how to implement and use the helpers and libraries. 

Is CodeIgniter Still Relevant? 

CodeIgniter has lost a lot of its popularity since Rick Ellis decided to give CodeIgniter to a new home.

On July 9th, 2013, Rick Ellis, the founder of EllisLab, announced he was looking for a new home for CodeIgniter. At that time CodeIgniter was arguably the most popular PHP framework. Click here to read Ellis’ online statement – EllisLab is seeking a new owner for codeigniter

That announcement set the stage for 2 things to happen. First, it threw the Codeigniter community into chaos because the future was unknown. Because of that a lot of developers fled to other PHP frameworks. After the dust cleared it appears most of the developers moved to Laravel. The second thing put in motion was CodeIgniter would become like the other PHP frameworks. Its footprint would increase, it would become more complicated and harder to learn. Ellis said it: “

PHP as a language is maturing, and CodeIgniter deserves to take advantage of its new stabilized features.”.

This announcement was a death sentence for CodeIgniter. CodeIgniter 4 contains Namespace and utilizes PHP Composer. These two are not necessarily needed and add to the learning curve.

CodeIgniter is slowly losing what sets it apart – being blazing fast, having a small footprint, and being extremely easy to install and learn. 

British Columbia Institute of Technology eventually took control of CodeIgniter

CodeIgniter continues to thrive though. 

If you are looking at CodeIgniter as a future skillset I’d caution you to look beyond. Having said that, there are still COBOL programmers out in the wild. I suspect there will be a need for CodeIgniter developers for a while. 

There is a lot to be learned from using CodeIgniter. Installing and learning how to use it is a great learning experience. I learned a lot from using CodeIgniter and looking at its source code to understand how things work.. 

I learned about Model-View-Controller, and how to modularize my code. When it was all said and done I was able to analyze CodeIgniter to gain the knowledge necessary to develop my small framework. 

Installing and using CodeIgniter will never be waste, it is a great learning opportunity, especially if you go into it with that mindset. 

Is CodeIgniter Dead?

Some think the current owners of CodeIgniter, the British Columbia Institute of Technology, is taking CodeIgniter down the wrong path. 

The downward spiral started the day Rick Ellis announced he was looking for a new home for CodeIgniter. At that very point, the future of CodeIgniter was thrown into chaos. A lot of CodeIgniter users migrated to other PHP frameworks. Most moved to Laravel.

CodeIgniter’s niche used to be that is had a small footprint, was blazing fast, was easy to install, and had a small learning curve. With version 4 in its early days, CodeIgniter is starting down the path towards trying to compete with the other PHP frameworks. Trying to compete with the other PHP frameworks is a mistake. 

CodeIgniter needs to go back to its roots of having a small footprint, being blazing fast, being easy to install, and having a small learning curve.

Google trends show CodeIgniter gets about 60 searches a day.

Image of Google Tends for CodeIgniter

Then when we look at the comparison of CodeIgniter vs Laravel, it looks like CodeIgniter is getting less than 10 searches a day and Laravel is around 75 searches a month.

Image of Google Trends for CodeIgniter vs Laravel

This indicates the interest in CodeIgniter is rather low.

Conclusion

  • This article is a brief look at CodeIgniter. 
  • There is two version – 3 and 4.
  • CodeIgniter is MVC.
  • It has helpers and libraries.
  • It is easy to learn and has a short learning curve of maybe 20 – 30 hours before the developer is ready to start building real-world applications.
  • CodeIgniter has a small footprint, is blazing fast, and easy to learn (version 3).
  • Version 4 is starting down the path away from the roots of CodeIgniter and moving out of its niche.

Keith Smith

Keith Smith was first introduced to programming at the University of Arizona in 1983. From 1986 to 1990 Keith was an xBase developer. In 2000 Keith built his first website from scratch by hand. In 2006 Keith become a freelance PHP developer. Today Keith wants to teach you everything he knows about PHP programming.

Recent Posts