DEV Community

Cover image for CodeIgniter Made Simple: Develop Powerful Web Applications
suraj kumar
suraj kumar

Posted on

CodeIgniter Made Simple: Develop Powerful Web Applications

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:

  1. Model – Manages data and database interactions.
  2. View – Handles the user interface and presentation.
  3. 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/';
Enter fullscreen mode Exit fullscreen mode

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!";
    }
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Update the controller to load the view:

public function hello_view()
{
    $this->load->view('hello_view');
}
Enter fullscreen mode Exit fullscreen mode

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
);
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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');
}
Enter fullscreen mode Exit fullscreen mode

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

  1. Follow MVC pattern strictly.
  2. Keep controllers lightweight — logic should go into models.
  3. Use autoload libraries for common functionalities.
  4. Keep views simple and avoid PHP logic in HTML.
  5. 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)