When you begin your journey into PHP frameworks, one of the best options to explore is CodeIgniter Tutorial. It is a lightweight, fast, and powerful PHP framework that helps developers build dynamic web applications with ease. Known for its simplicity, flexibility, and small footprint, CodeIgniter is especially useful for beginners who want to learn how frameworks work without dealing with too much complexity.
In this tutorial, we’ll cover the basics of CodeIgniter, its features, installation steps, and a simple example to help you get started.
What is CodeIgniter?
CodeIgniter is an open-source PHP framework that follows the Model-View-Controller (MVC) architectural pattern. It was created to provide developers with a toolkit to quickly build robust web applications without reinventing the wheel.
- Model handles the database operations.
- View is responsible for the user interface (UI).
- Controller connects the Model and the View by processing user requests.
By separating these concerns, CodeIgniter makes code clean, maintainable, and reusable.
Why Use CodeIgniter?
Here are some reasons why developers prefer CodeIgniter:
- Lightweight Framework – The core system requires very little overhead.
- Fast Performance – CodeIgniter is known for speed and efficiency.
- Easy Learning Curve – Perfect for beginners transitioning from raw PHP.
- Built-in Libraries – Comes with libraries for session handling, form validation, email, and database queries.
- Security Features – Helps protect against CSRF, XSS, and SQL injection attacks.
- Clear Documentation – CodeIgniter has excellent documentation, making it beginner-friendly.
Installing CodeIgniter
Follow these steps to install CodeIgniter on your system:
Step 1: Download CodeIgniter
- Visit the official site: https://codeigniter.com
- Download the latest stable version of CodeIgniter.
Step 2: Extract the Files
- Extract the downloaded ZIP file into your project folder (e.g.,
htdocs/codeigniter
if using XAMPP).
Step 3: Test the Installation
- Open your browser and go to:
http://localhost/codeigniter/
- If the installation is successful, you’ll see the CodeIgniter welcome screen.
CodeIgniter Folder Structure
Some important folders to understand:
- application/ → Contains your project files (controllers, models, views).
- system/ → Contains the core framework files (do not modify).
- index.php → The entry point of your application.
Inside the application folder:
- controllers/ – Contains controller classes.
- models/ – Contains database interaction files.
- views/ – Contains the user interface templates.
Your First CodeIgniter Application
Let’s create a simple “Hello World” application.
Step 1: Create a Controller
Go to application/controllers/
and create a file named Hello.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello extends CI_Controller {
public function index() {
$this->load->view('hello_view');
}
}
?>
Step 2: Create a View
Go to application/views/
and create a file named hello_view.php
<!DOCTYPE html>
<html>
<head>
<title>Hello CodeIgniter</title>
</head>
<body>
<h1>Welcome to CodeIgniter!</h1>
<p>This is your first CodeIgniter application.</p>
</body>
</html>
Step 3: Run the Application
Open your browser and enter:
http://localhost/codeigniter/index.php/hello
You should see “Welcome to CodeIgniter!” displayed.
Working with Models (Database Example)
Suppose you have a database named testdb with a table users.
Step 1: Create a Model
Go to application/models/
and create a file named User_model.php
<?php
class User_model extends CI_Model {
public function getUsers() {
$query = $this->db->get('users');
return $query->result();
}
}
?>
Step 2: Use Model in Controller
Update Hello.php
controller:
<?php
class Hello extends CI_Controller {
public function index() {
$this->load->model('User_model');
$data['users'] = $this->User_model->getUsers();
$this->load->view('hello_view', $data);
}
}
?>
Step 3: Display Data in View
Update hello_view.php
:
<h1>User List</h1>
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user->name; ?></li>
<?php endforeach; ?>
</ul>
This will display all users from the database.
Advantages of CodeIgniter
- Easy to set up and start coding.
- Follows MVC structure for clean code.
- Offers built-in form validation and session handling.
- Compatible with multiple databases (MySQL, PostgreSQL, SQLite, etc.).
- Works well on shared hosting servers due to its lightweight nature.
Conclusion
CodeIgniter is an excellent PHP framework for beginners who want to move beyond raw PHP development. Its lightweight design, simple learning curve, and powerful features make it an ideal choice for small to medium-sized projects. By learning CodeIgniter Tutorial, you’ll gain a deeper understanding of how modern PHP frameworks work while keeping things simple and manageable.
Whether you’re building a small blog, a dynamic website, or a scalable web application, CodeIgniter gives you the tools to develop quickly and efficiently.
Top comments (0)