What is new in CodeIgniter 4
CodeIgniter 4 is a complete rewrite of the framework, not just a simple version update. It was built from the ground up to comply with modern PHP standards and practices. While it retains the lightweight and fast performance that CodeIgniter is known for, the architecture has changed significantly to offer better security and modularity. The framework now relies on namespaces, strict autoloading, and a command-line interface to help developers build robust applications.
Server Requirements
Because CodeIgniter 4 uses modern PHP features, the hosting requirements have changed compared to previous versions. It requires a more recent version of PHP and specific extensions enabled on the server.
| Requirement | Details |
|---|---|
| PHP Version | Requires modern PHP versions (7.4+ or 8.1+ depending on the specific release). |
| Required Extensions | intl, mbstring, json, mysqlnd, xml, curl |
| Supported Databases | MySQL (5.1+), PostgreSQL, SQLite3, SQLSRV |
Application Structure and Security
The file structure has been reorganized to improve security. The most notable change is the introduction of a public directory. This folder acts as the web root, meaning your application code (Controllers, Models) and system files are placed outside the publicly accessible area. This prevents direct access to your core files by visitors.
app/-- Contains your application code (Controllers, Models, Views).public/-- The web root containing the browser-accessibleindex.phpand assets.writable/-- Holds temporary files like cache, logs, and session data.system/-- The core framework files.
Namespaces and Autoloading
CodeIgniter 4 fully adopts PSR-4 standards for autoloading. You no longer need to manually load libraries or models using super-objects like in previous versions. Instead, you use standard PHP namespaces to import the classes you need.
Example of a Controller using namespaces:
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
class Home extends Controller {
public function index() {
return view('welcome_message');
}
}
CLI Tools with Spark
The framework now includes a built-in command-line tool called Spark. This tool helps developers perform tasks such as clearing caches, running database migrations, and creating boilerplates for controllers and models without writing files manually.
Common Spark commands:
# Start a local development server php spark serve
Create a new controller file
php spark make:controller User
Run database migrations
php spark migrate
Models and Entities
The Model class has been enhanced with automatic CRUD (Create, Read, Update, Delete) methods, soft deletes, and validation rules. Additionally, CodeIgniter 4 introduces Entities. While Models handle the database connection, Entities represent a single row of data as an object, allowing you to format data elegantly before it is saved or displayed.
// Example of finding a user with ID 15
$userModel = new \App\Models\UserModel(); $user = $userModel->find(15);
// The result is an object (Entity) or array depending on configuration
echo $user->email;
Environment Configuration
Configuration has become environment-aware through the use of .env files. You can create different configuration files for development, testing, and production environments. This allows you to manage database credentials and debug settings securely without modifying the core configuration files in your application code.