Installing CodeIgniter: A Beginner's Guide to PHP Framework Setup
Step-by-step guide to installing and configuring CodeIgniter 4 — from Composer to your first controller, view, and route in under 30 minutes.
CodeIgniter is a lightweight, powerful PHP framework that helps you build web applications quickly using the Model-View-Controller (MVC) pattern. It is known for its simplicity, speed, and excellent documentation.
Installation
# Recommended: via Composer
composer create-project codeigniter4/appstarter my-project
# Set directory permissions (Linux / macOS)
chmod -R 755 my-project
chmod -R 777 my-project/writable
# Configure base URL — edit app/Config/App.php
public $baseURL = 'http://localhost/my-project/';
# Set environment — edit .env
CI_ENVIRONMENT = development
Directory Structure
| Directory | Purpose |
|---|---|
app/ | Your application — Controllers, Models, Views |
public/ | Public entry point (index.php) and static assets |
writable/ | Logs, cache, and session files |
tests/ | Unit and feature tests |
vendor/ | Composer dependencies |
Your First Controller
<?php
// app/Controllers/Home.php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
return view('welcome_message');
}
public function about()
{
$data = [
'title' => 'About Us',
'message' => 'Welcome to our website!',
];
return view('about', $data);
}
}
Your First View
<!-- app/Views/about.php -->
<!DOCTYPE html>
<html>
<head><title><?= esc($title) ?></title></head>
<body>
<h1><?= esc($title) ?></h1>
<p><?= esc($message) ?></p>
</body>
</html>
Routing
// app/Config/Routes.php
$routes->get('/', 'Home::index');
$routes->get('/about', 'Home::about');
$routes->get('/user/(:num)', 'User::profile/$1');
Database Configuration
# .env
database.default.hostname = localhost
database.default.database = my_database
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi
database.default.port = 3306
// Using the database in a controller
$db = \Config\Database::connect();
$query = $db->query("SELECT * FROM users");
$results = $query->getResultArray();
Why CodeIgniter for Beginners
- Simple MVC — easy to understand and reason about
- Excellent docs — comprehensive user guide with real examples
- Lightweight — small footprint, fast responses even on shared hosting
- Built-in libraries — email, sessions, form validation, file upload out of the box
- Active community — answers to most problems are one search away