CodeIgniter
The CodeIgniter Framework
For full documentation visit codeigniter.com.
Over 10 Years | Multiple Projects | Not Preferred
CodeIgniter is an open-source PHP framework built for web developers who need a simple, lightweight toolkit to build full-featured web applications. It follows the Model-View-Controller (MVC) structural pattern, minimizing code footprints while delivering exceptional speed and performance.
The following step-by-step example demonstrates how to set up a basic CodeIgniter 4 project, configure a route, use a Model to query data, process it in a Controller, and display it in a View.
- Step 1: Install and Run CodeIgniter
The standard way to initialize a project is via Composer. Run this command in your terminal:
composer create-project codeigniter4/appstarter sample-app
Navigate into your folder and start the local development server:
cd sample-app
php spark serve
Your website is now available locally at http://localhost:8080.
- Step 2: Configure the Database
Open the .env file in your root folder. Provide your local credentials:
database.default.hostname = localhost
database.default.database = sample_db
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi
Assume you have a database table named books with fields id (INT), title (VARCHAR), and author (VARCHAR).
- Step 3: Create the Model
Models handle data structures and database operations. Create a file named BookModel.php inside the app/Models/ folder:
<?php
namespace App\Models;
use CodeIgniter\Model;
class BookModel extends Model
{
protected $table = 'books';
protected $primaryKey = 'id';
protected $allowedFields = ['title', 'author'];
}
- Step 4: Create the Controller
Controllers act as the connector between the business logic and presentation layers. Create a file named Books.php inside the app/Controllers/ folder:
<?php
namespace App\Controllers;
use App\Models\BookModel;
class Books extends BaseController
{
public function index()
{
$model = new BookModel();
// Fetch all records from the 'books' table
$data['books'] = $model->findAll();
$data['page_title'] = "My Library Collections";
// Send data array to the view
return view('books_view', $data);
}
}
- Step 5: Create the View
Views are responsible for rendering formatting and markup (HTML/CSS) to the user. Create a file named books_view.php inside the app/Views/ folder:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= esc($page_title); ?></title>
</head>
<body>
<h1><?= esc($page_title); ?></h1>
<?php if (!empty($books) && is_array($books)): ?>
<ul>
<?php foreach ($books as $book): ?>
<li><strong><?= esc($book['title']); ?></strong> by <?= esc($book['author']); ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No books found in the inventory.</p>
<?php endif; ?>
</body>
</html>
- Step 6: Map the URL Route
To tell CodeIgniter which controller to run when a user visits a URL, configure the routing file. Open app/Config/Routes.php and append your route mapping:
$routes->get('library', 'Books::index');
Now, navigate your web browser to http://localhost:8080/library. CodeIgniter will process the router instruction, request data via BookModel, handle variables via the Books controller, and display your styled records using books_view.
Very quick to setup a new project with!
CodeIgniter is among the original MVC frameworks. There are no special commands as in other PHP based frameworks, but projects are still very quick to build out and customize.
Development Frameworks
JS Frameworks
CSS Frameworks
More Frameworks
Social Media
---
title: CodeIgniter
text: The CodeIgniter Framework
image: /assets/svg/codeigniter1.svg
link:
target:
---