Introduction
In the world of web development, PHP frameworks have become essential tools for developers seeking efficiency, scalability, and maintainability. CodeIgniter Tutorial is a lightweight, powerful PHP framework that simplifies the development of dynamic web applications. Known for its speed and minimal configuration, CodeIgniter allows developers to build secure and high-performance websites quickly. This tutorial will guide you step by step on how to understand CodeIgniter, set up your environment, and develop web applications that are both efficient and scalable.
What is CodeIgniter?
CodeIgniter is an open-source PHP framework based on the Model-View-Controller (MVC) architectural pattern. MVC separates your application into three main components:
- Model – Manages data and database interactions.
- View – Handles the user interface and presentation.
- Controller – Coordinates requests between Model and View.
This separation ensures cleaner, more organized code, which is easier to maintain and scale. CodeIgniter’s lightweight footprint and extensive documentation make it ideal for both beginners and experienced developers.
Why Choose CodeIgniter?
CodeIgniter is favored for several reasons:
Lightweight and Fast: Minimal configuration and small footprint.
Easy to Learn: Straightforward syntax and MVC structure.
Built-in Libraries: Includes libraries for sessions, forms, validation, email, and more.
Secure: Built-in protection against SQL injection, XSS, and CSRF attacks.
Flexible and Scalable: Ideal for small projects and enterprise-level applications.
Strong Community Support: Extensive resources, tutorials, and forums.
Step 1: Setting Up CodeIgniter
1. Download CodeIgniter
Visit the official website: https://codeigniter.com and download the latest version of CodeIgniter.
2. Extract Files
Extract the downloaded zip file to your web server’s root directory. For example:
-
XAMPP:
C:\xampp\htdocs\codeigniter_app
-
MAMP:
/Applications/MAMP/htdocs/codeigniter_app
3. Configure Base URL
Open application/config/config.php
and set your base URL:
$config['base_url'] = 'http://localhost/codeigniter_app/';
4. Run the Application
Open your browser and visit http://localhost/codeigniter_app/
. You should see the CodeIgniter welcome page.
Step 2: Creating Your First CodeIgniter Controller
Controllers handle requests and determine what content to display.
Create a file Welcome.php
in application/controllers/
:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function hello()
{
echo "Hello, CodeIgniter!";
}
}
Now visit http://localhost/codeigniter_app/index.php/welcome/hello
to see the output.
Step 3: Creating Views
Views are used to present information to users.
Create a file hello_view.php
in application/views/
:
<!DOCTYPE html>
<html>
<head>
<title>Hello CodeIgniter</title>
</head>
<body>
<h1>Welcome to CodeIgniter!</h1>
<p>This is your first view.</p>
</body>
</html>
Update the controller to load the view:
public function hello_view()
{
$this->load->view('hello_view');
}
Visit http://localhost/codeigniter_app/index.php/welcome/hello_view
.
Step 4: Working with Models and Database
1. Configure Database
Open application/config/database.php
and set your database credentials:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci_database',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE
);
2. Create a Model
Create a file Employee_model.php
in application/models/
:
<?php
class Employee_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_employees() {
$query = $this->db->get('employees');
return $query->result_array();
}
}
3. Use Model in Controller
public function employees()
{
$this->load->model('Employee_model');
$data['employees'] = $this->Employee_model->get_employees();
$this->load->view('employees_view', $data);
}
4. Create a View to Display Data
Create employees_view.php
in application/views/
:
<!DOCTYPE html>
<html>
<head>
<title>Employees List</title>
</head>
<body>
<h1>Employees</h1>
<ul>
<?php foreach($employees as $employee): ?>
<li><?php echo $employee['name']; ?> - <?php echo $employee['position']; ?></li>
<?php endforeach; ?>
</ul>
</body>
</html>
Step 5: Form Handling and Validation
CodeIgniter makes it easy to handle forms with built-in validation libraries.
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run() === FALSE) {
$this->load->view('my_form');
} else {
$this->load->view('form_success');
}
This ensures your input is validated before storing in the database.
Step 6: Security Features
CodeIgniter provides built-in protection against:
- SQL Injection: Automatic escaping in queries.
-
Cross-Site Scripting (XSS):
html_escape()
for user input. -
Cross-Site Request Forgery (CSRF): Enable in
config.php
.
Step 7: Best Practices for CodeIgniter
- Follow MVC pattern strictly.
- Keep controllers lightweight — logic should go into models.
- Use autoload libraries for common functionalities.
- Keep views simple and avoid PHP logic in HTML.
- Use environment configuration for different environments (development, production).
Conclusion
CodeIgniter Tutorial is a powerful yet simple PHP framework that accelerates web development. Its lightweight structure, minimal configuration, and MVC architecture make it ideal for developers of all levels. From creating controllers and views to handling databases, forms, and security, CodeIgniter allows you to build robust and scalable web applications efficiently.
By practicing the steps outlined in this tutorial, you can confidently develop web apps, implement advanced features, and maintain clean, organized code. Whether you are building small projects or enterprise applications, CodeIgniter is a framework that combines speed, simplicity, and power in one package.
Top comments (0)