Promo Offers Black Friday Offers:
OpenVS VPS up to 60% OFF for 12 months Learn More Semi-Dedicated up to 86% OFF for 3 months Learn More
  • Your Cart is Empty
  • Cart
  • Log In

CodeIgniter

What is CodeIgniter?

CodeIgniter is an open source framework for creating full-featured PHP-based web sites and applications. It is based on the MVC (Model-View-Controller) architecture and is compatible with standard shared hosting accounts, various PHP versions (including PHP4 and PHP5) and configurations. Plus, you don't have to use a command line. URLs are mapped to a particular function within a controller by means of routing. CodeIgniter also supports scaffolding which represents an automated way of generating views based on the models. Although scaffolding is designed for simple prototyping, CodeIgniter takes it a step further by requiring a keyword in the URL in order to access it at all. This modern MVC framework renders the creating of PHP applications much easier. A large portion of the work, such as loading libraries, getting data from the model, rendering the view, etc., is done inside the controller. CodeIgniter provides classes for FTP, email, file uploading, XMLRPC, zip encoding. You are not required to give names to you database tables or to name your models after your tables.

Latest version: 1.7.1
PHP4
PHP5
MVC
Multiple DB
ORM
DB Objects
Templates
Caching
Validation
Ajax
Auth Module
Modules
Cost FREE

CodeIgniter - installation and requirements

CodeIgniter has a very small footprint - it is just about 2 MB, including the documentation which you can delete later. Plus, it works with all servers and web hosting accounts that provide PHP version 4.3.2 or newer. CodeIgniter requires a database for most web applications. Current supported ones are MySQL (4.1+), MySQLi, MS SQL, Postgres, Oracle, SQLite, and ODBC.

To install CodeIgniter, you need to download and unzip the package and then upload its folders and files to your server. Normally, the index.php file will be in your web root folder. Once uploaded, you need to open the application/config/config.php file with a text editor and set your base URL. If you intend to use encryption or sessions you have to set your encryption key too. In order to set up a database for your CodeIgniter PHP framework you need to open the application/config/database.php file with a text editor and set your database using the settings provided with your web hosting account. You can rename the system folder to something more private so as to increase security by hiding the location of your CodeIgniter files. After you rename it you need to open your main index.php file and set the $system_folder variable at the top of the page with the new name you have chosen.

Models, Views, Controllers - CodeIgniter's MVC architecture

CodeIgniter is based on the MVC architecture whose main point is that the views have to be separated from the back end of a given application. The back and the front end, on the other hand, are connected using controllers. The separation between the front and the back end of a web application makes the code readable and easier to understand.

Models

Models have to do with connecting to a database and performing Create, Read, Update and Delete operations. Most often, there is a model for each table in the database. It is named tablename_model.php. Models and controllers contain an uppercased constructor, which often bears the same name as the page itself. The models have their own functions which can be called as soon as you create a specific instance of the given model.

As an example we will create a simple form that submits data to a MySQL database and then retrieves the already submitted data.

For that purpose, we first need to create a table where the data will be submitted to. This can be done by executing the following SQL statement through phpMyAdmin or directly through the MySQL console.

CREATE TABLE `form` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(50) default NULL,
`data` text,
);

Then our model in CodeIgniter will be placed in application/models/form_model.php

<?php
class Form_model extends Model {
function Formmodel() {
// load the parent constructor
parent::Model();
}
function submit_posted_data() {
// db is initialized in the controller, to interact with the database.
$this->db->insert('form',$_POST);    
}
function get_all_data() {
// again, we use the db to get the data from table ‘form’
$data['result']=$this->db->get('form');
return $data['result'];
}
}
?>

Controllers

The controllers connect the back end and the front end. In the example above, the Form_model functions performed the INSERT and SELECT statements. The controller will call these functions to output what it needs to display to a view. All database-related functions in CodeIgniter are set and called using a model. The front end is displayed and managed by the view. The controller’s structure resembles that of the model and its name is in lowercase. For example, the form.php controller communicates between the form_model.php model and the individual views.

In our example of the Controller in CodeIgniter will be application/controllers/form.php, which will contain:

<?php
class Form extends Controller {
function Form(){
// load Controller constructor
parent::Controller();
// load the model we will be using
$this->load->model('form_model');
// load the database and connect to MySQL
$this->load->database();
// load the needed helpers
$this->load->helper(array('form','url'));
}
//Display the posted entries
function index() {
$data['title']='Form Data';
//use the model to get all entries
$data['result'] = $this->form_model->get_all_data();
// load 'forms_view' view
$this->load->view('forms_view',$data);
}         
//Process the posted form
function submit() {
//use the model to submit the posted data
$this->form_model->submit_posted_data();
redirect('form');
}
}
?>

In CodeIgniter, you can have a separate controller for each section of your website. On the other hand, the pages within that section can have their own function in the controller. So, when you go to a controller, for example www.you-ntc-domain.com/index.php/form, the index function of the form controller is called. Still, you can have a page that submits data, for example, by creating a function submit() and post it to www.you-ntc-domain.com/index.php/form/submit to execute that function.

In brief, the controller creates a new instance of the model, does certain functions and passes the results to the view so as to display them on the web browser.

Views

The views display what you in fact see. You can set up a view in many ways. You can have only one view to represent different pages based on the information that the controllers pass to it, or instead have an entirely different view for each web page.

A view resembles a regular HTML page with doctype, head and body tags. Often header and footer views load before and after the content view you are using. The view outputs information that the controller passes to it. Everything inside the $data array is passed to the view, which we can reference using the object’s name as a variable. For example, if we pass $data['price'] to the view, we can reference that using $price in the view itself; if we pass $data['description'], we can reference it in the view using $description, etc. The $data array itself can contain other arrays as well. For example, if the $data array contains an array named 'productinfo', we can have $productinfo['price'] or $productinfo['shipping'] displayed in the view. Below is the view that we use in our example form application/views/forms_view.php:

<html>
<head>
<title><?=$title;?></title>
</head>
<body>
<h1><?=$title;?></h1>
<table border='1'>
<tr>
<th>Title</th>
<td>Entry</td>
</tr>
<?php foreach($result->result_array() as $entry):?>
<tr>
<th><?=$entry['title'];?></th>
<td><?=$entry['body'];?></td>
</tr>
<?php endforeach;?>
</table>
<?php echo form_open('form/submit'); ?>
<br><br>
Titile<br>
<input type="text" name="Title"><br>
Entry<br>
<input type="text" name="data">
<input type="submit" value="New">
</form>
</body>
</html>

Check out the alternative PHP syntax. Instead of an echo statement to send out the price, you can simply use <?=$something_to_echo?>. However, this requires that short tags are enabled in php.ini. Still, CodeIgniter can rewrite them so that they work on your web hosting server if you change $config['rewrite_short_tags'] = TRUE in system/application/config.php at the bottom.

Resources: