How to install and setup codeigniter framework

CodeIgniter is a powerful open-source PHP framework with a very small footprint, created by Rick Ellis in 2006.

I will present you a step-by-step instruction how to set up development environment from a codeigniter framework.In this tutorial I am using CodeIgniter 3.1.9.

1.) Download Codeigniter:

Download Codeigniter framework.

2.) Unzip the File:

Now Unzip the folder inside C:\xampp\htdocs. You can rename folder name like myapp .

3.) Initial testing:

Everything is ready so finally let’s launch the Codeigniter app.Now let’s move on to browser and type in URL http:\\localhost\myapp.You will get interface as showing in below.

4.) Database Configuration:

Let’s now setup the database located in myapp/application/config/database.php. Change the username,password and database name.

5.) Mysql database table:

Next,Create database table.In my case database name is myapp and the table name is tbl_user. You can create table by running below script.


CREATE TABLE `tbl_user` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`mobile` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tbl_user`
ADD PRIMARY KEY (`id`);
ALTER TABLE `tbl_user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

6.) Creating View:

Next, create a view file in your myapp/application/views directory called registerview.php with this code.

registerview.php

<html>
<head>
<title>Register</title>
</head>
<body>
<style>
h3
{
font-family: Verdana;
font-size: 18pt;
font-style: normal;
font-weight: bold;
color:red;
text-align: center;
}

table{
font-family: Verdana;
color:black;
font-size: 12pt;
font-style: normal;
font-weight: bold;
text-align:left;
border-collapse: collapse;
}
</style>
<h3>Registration</h3>

<form name="registerview" action="<?php echo base_url(); ?>register" method="post">
<table align="center" cellpadding = "5">
<tr>
<td>Name</td>
<td><input type="text" name="name" maxlength="50"/></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" maxlength="10"/></td>
</tr>
<tr>
<td>Adress</td>
<td><input type="text" name="address" maxlength="100"/></td>
</tr>
<tr>
<td colspan="5" align="center">
<input type="submit" value="Register"/></td>
</tr>
</table>
</form>
</body>
</html>

7.) Creating Controller:

Next, create a controller in your myapp/application/controller directory called RegisterController.php with this code.Make sure first letter of controller’s name in Uppercase as like i mention below.

RegisterController.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class RegisterController extends CI_Controller {
public function index(){
$this->load->view('registerview');
}
public function register(){
$this->load->model('registermodel');
$data = array(
'name'=>$this->input->post('name'),
'mobile'=>$this->input->post('mobile'),
'address'=>$this->input->post('address')
);
$this->registermodel->save($data);
redirect('registercontroller');
}
}

8.) Creating Model:

Next, create a model in your myapp/application/model directory called RegisterModel.php with this code. Make sure first letter of model’s name in Uppercase as like i mention below.

RegisterModel.php


<?php
class RegisterModel extends CI_Model {
private $table_name = 'tbl_user';
public function __construct() {
$this->load->database();
}
public function save($data){
$return = $this->db->insert($this->table_name, $data);
return $this->db->insert_id();
}
}
?>

9.) Next, load the library and helper in autoload.php file located in myapp/application/config/autoload.php. 


$autoload['libraries'] = array('database');
$autoload['helper'] = array('url','html','file');

10.) Route Configuration:

Change the default controller in routes.php file located in myapp/application/config/routes.php. 


$route['default_controller'] = 'registercontroller';
$route['register'] = 'registercontroller/register';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Now,move on to browser and type in URL http:\\localhost\myapp.You will get interface as showing in below.

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – let us know. We’d love to help!

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , , , , ,