CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.
CodeIgniter is for you if:
www folder
if you use Wampserver or htdocs
if you use Xampp.index.php
serves as the front controller, initializing the base resources needed to run CodeIgniter.CodeIgniter is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is apart from the PHP.
.htaccess
file
update the .htaccess
file of the application folder to include this code:1RewriteEngine on
2RewriteCond %{REQUEST_FILENAME} !-f
3RewriteCond %{REQUEST_FILENAME} !-d
4RewriteRule .* index.php/$0 [PT,L]
CodeIgniter has one primary config file, located at application/config/config.php
1/* Base URL Configuration */
2$config['base_url'] = 'http://localhost/test/';
3
4 /* Remove index file from the URL */
5$config['index_page'] = '';
The autoload file allows libraries, helpers, and models to be initialized automatically every time the system runs.
1/* Libraries load */
2$autoload['libraries'] = array('session','database','form_validation');
3
4 /* Helpers load */
5$autoload['helper'] = array('url','text','form','date');
The database file lets the developer store database connection values (username, password, database name, etc.).
1$db['default'] = array(
2 'dsn' => '',
3 'hostname' => 'localhost',
4 'username' => 'root',
5 'password' => '',
6 'database' => 'test',
7 'dbdriver' => 'mysqli',
8 'dbprefix' => '',
9 'pconnect' => FALSE,
10 'db_debug' => (ENVIRONMENT !== 'production'),
11 'cache_on' => FALSE,
12 'cachedir' => '',
13 'char_set' => 'utf8',
14 'dbcollat' => 'utf8_general_ci',
15 'swap_pre' => '',
16 'encrypt' => FALSE,
17 'compress' => FALSE,
18 'stricton' => FALSE,
19 'failover' => array(),
20 'save_queries' => TRUE
21);
Now let's try out an example. We want to show a list of users from a data table. Let's start with the model.
id | username | password |
---|---|---|
1 | Adib | 123456 |
2 | Wadii | abcdef |
3 | Ayoub | azerty |
We see that there are a few properties to account for with each user. For example, every user has an id
, username
, and password
. These will become our Model attributes. Remember that the Model handles the back-end, per se. Therefore, any operations dealing with table manipulation should be completed in this class (or its related non-View, non-Controller classes).
1class User_model extends CI_Model {
2
3 public $id;
4 public $username;
5 public $password;
6
7 function getId() {
8 return $this->id;
9 }
10
11 function getUsername() {
12 return $this->username;
13 }
14
15 function getPassword() {
16 return $this->password;
17 }
18
19 function setId($id) {
20 $this->id = $id;
21 }
22
23 function setUsername($username) {
24 $this->username = $username;
25 }
26
27 function setPassword($password) {
28 $this->password = $password;
29 }
30
31
32 function __construct() {
33 parent::__construct();
34 }
35
36 public function user_list() {
37 return
38 $this->db->select('*')
39 ->from('user')
40 ->order_by('id_user', 'DESC')
41 ->get()
42 ->result_array();
43 }
44}
Next, we will consider the controller. Remember that the controller relays information and requests between the model and the view. We want to transmit all changes going from model to view and vice versa while keeping abstraction barriers intact (i.e. while keeping the user away from the inner pieces of the model itself).
1class user extends CI_Controller {
2
3 public function __construct() {
4 parent::__construct();
5 $this->load->model('user_model');
6 }
7
8 public function user_list() {
9 $data = array();
10 $tab = $this->User_model->user_list();
11 $this->load->view('list', $data);
12 }
13}
The constructor for the controller loads the model
from user_model
in order to retrieve the values in the table itself. As seen by the user_list()
function, the controller is going between the User_model
class and loading view
elements.
Lastly, we have the view which dictates what the user sees. We will use HTML to place users into each spot in the HTML table (denoted by the <table>
tags). Notice that we use $tab
in the foreach
. This tells the view to construct a table containing values from the user_list()
. Furthermore, by using foreach
we can expand the table if more users are added.
1<html>
2<head>
3<title>Users list</title>
4</head>
5<body>
6<table>
7<thead>
8<tr>
9<th>ID</th>
10<th>Username</th>
11<th>Password</th>
12</tr>
13</thead>
14<tbody>
15<?php
16 foreach($tab as $key => $value){
17 echo "<tr>" ;
18 echo "<td>";
19 echo $value["id"] ;
20 echo "</td>";
21 echo "<td>";
22 echo $value["username"] ;
23 echo "</td>";
24 echo "<td>";
25 echo $value["password"] ;
26 echo "</td>";
27 echo "</tr>" ;
28 }
29?>
30</tbody>
31</table>
32</body>
33</html>
And with that, we have a responsive, script-based table that shuffles between Model, View, and Controller.
In this guide, we covered the basics of application architecture, Model-View-Controller, and writing short, quick scripts using CodeIgniter. Hopefully, this guide demonstrated CodeIgniter's very straightforward setup and configuration process as well as its ease of use.