CodeIgniter

CodeIgniter is an Application Development Framework (ADF). An ADF is a software designed to facilitate software development by integrating architecture patterns such as MVC and providing libraries to facilitate database access, session management, access control and other common development tasks. One goal of ADF is to provide pre-packaged code and promote code reuse based on sound software engineering principles.

CodeIgniter is a PHP Web ADF. It facilitates web development. ADFs come in many shapes and sizes, each with its own strengths and weaknesses. If you will be involved in a significant project, it would make sense to read up on the different Web ADFs before you start coding. The two PHP Web ADFs that I have used are Zend and CodeIgniter. Zend is very powerful but it is bulky and has a relatively steep learning curve. CodeIgniter is small, fast, easy to install, learn, use and configure. In addition, the documentation is just fantastic.

CodeIgniter is ideal for starter yet suitable for small and medium sized projects.

There are many different frameworks and we all have different tastes and project requirements. When it comes to choosing an ADF, check out the facts, and ignore the emotional comments of other programmers. You will find best suited for your needs.

Download and Install

CodeIgniter is free and open source. You can download it from http://ellislab.com/codeigniter. To install, following the instruction at here.

Hello World

Navigate to the your CodeIgniter installation location.

Open: application/config/routes.php Change the following code

$route['default_controller'] = "welcome";

to

$route['default_controller'] = "market";

Passing values from controller to view

Continuing with the example from the previous page.

Open: application/controllers/market.php Change the code as follows and save it

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

class Market extends CI_Controller {

   public function index() 
   {
       $data['message'] = $this->hello('fr');
       $this->load->view('market_view', $data);
   }

   function hello($lang) 
   {
       if ($lang == 'fr') {
           return "Bonjour le monde";
       } else {
           return "Hello World";
       }
   }
}

Open: application/controllers/market_view.php Change the code as follows and save it

<html>
<head>
<title>hello world</title>
</head>
</body>
<h3><?php echo $message; ?></h3>
</body>
</html>

Coding business logic in Model

Go to: application/models Create a new file message.php. Copy paste the following code and save it.

Class Message extends CI_Model {

   public function hello($lang) 
   {
       if ($lang == 'fr') {
           return "Bonjour le monde";
       } else {
           return "Hello World";
       }
   }
}

Open: application/controller/market.php Modify the code as follows and save the file:

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

class Market extends CI_Controller {

   public function index() 
   {
       $this->hello();
   }

   function hello() 
   {
       $this->load->model("message");
       $data['message'] = $this->message->hello("en");
       $this->load->view('market_view', $data);
   }
}

When you run this code, you will get the "Hello World" message. The index function in the controller is called by default from where we call hello(). In hello, we load the message model, pass the value "en" to its hello function, and display the output in the view.

Form Processing

This page shows a very basic form processing example.

Go to: application/views Create a new file header.php. Copy paste the following code and save it.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Stock Market Analysis Tool 3.0</title>
<style type="text/css">
::selection{ background-color: #E13300; color: white; }
::moz-selection{ background-color: #E13300; color: white; }
::webkit-selection{ background-color: #E13300; color: white; }

body {
   background-color: #fff;
   margin: 40px;
   font: 13px/20px normal Helvetica, Arial, sans-serif;
   color: #4F5155;
}

a {
   color: #003399;
   background-color: transparent;
   font-weight: normal;
}

h1 {
   color: #444;
   background-color: transparent;
   border-bottom: 1px solid #D0D0D0;
   font-size: 19px;
   font-weight: normal;
   margin: 0 0 14px 0;
   padding: 14px 15px 10px 15px;
}

code {
   font-family: Consolas, Monaco, Courier New, Courier, monospace;
   font-size: 12px;
   background-color: #f9f9f9;
   border: 1px solid #D0D0D0;
   color: #002166;
   display: block;
   margin: 14px 0 14px 0;
   padding: 12px 10px 12px 10px;
}

code.message {
   font-family: Consolas, Monaco, Courier New, Courier, monospace;
   font-size: 12px;
   background-color: #ffffff;
   border: 1px solid #D0D0D0;
   color: #00ff00;
   display: block;
   margin: 14px 0 14px 0;
   padding: 12px 10px 12px 10px;
}

#body{
   margin: 0 15px 0 15px;
}

p.footer{
   text-align: right;
   font-size: 11px;
   border-top: 1px solid #D0D0D0;
   line-height: 32px;
   padding: 0 10px 0 10px;
   margin: 20px 0 0 0;
}

#container{
   margin: 10px;
   border: 1px solid #D0D0D0;
   -webkit-box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Codeigniter: form processing example</h1>
<div id="body">

Open: application/controller/market.php Modify the code as follows and save the file:

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

class Market extends CI_Controller {

   public function index() 
   {
       $this->hello();
   }

   function hello() 
   {
       $this->load->model("message");
       $data['message'] = $this->message->hello("en");
       $this->load->view('market_view', $data);
   }
}

When you run this code, you will get the "Hello World" message. The index function in the controller is called by default from where we call hello(). In hello, we load the message model, pass the value "en" to its hello function, and display the output in the view.

Technologies: