Introduction
CodeIgniter 4 is a complete rewrite of the original CodeIgniter framework, modernized with PHP 8 support, namespaces, PSR compliance, and a built-in CLI tool called Spark. It maintains the framework's tradition of a small footprint and clear documentation while adding features expected in a modern PHP framework like an ORM, migrations, and a testing library.
What CodeIgniter 4 Does
- Provides MVC architecture with controllers, models, and view templates
- Includes a query builder and simple ORM for database operations
- Runs a built-in development server via
php spark serve - Supports database migrations, seeding, and schema management
- Offers a CLI tool (Spark) for code generation and common tasks
Architecture Overview
CodeIgniter 4 follows the Model-View-Controller pattern with a front controller that routes all requests through public/index.php. The routing system supports both defined routes and automatic controller method discovery. The framework loads only the components needed for each request, keeping memory usage low. Services are accessed through a service locator pattern, and configuration is handled via PHP classes rather than arrays or YAML files.
Self-Hosting & Configuration
- Install with Composer:
composer create-project codeigniter4/appstarter - Configure the database in
app/Config/Database.phpwith connection details - Set the base URL and environment in
.env(development, testing, production) - Run migrations with
php spark migrateto apply database schema changes - Deploy to Apache or Nginx with a standard PHP-FPM configuration
Key Features
- Extremely small framework core with fast request processing
- Built-in RESTful resource routing for API development
- Shield authentication library for login, registration, and 2FA
- Honeypot and CSRF protection included by default
- Comprehensive testing library with mock HTTP requests and database testing
Comparison with Similar Tools
- Laravel — more features and larger ecosystem, heavier runtime footprint
- Symfony — enterprise-grade component library, steeper learning curve
- Slim — micro framework for APIs only, no ORM or view layer
- CakePHP — convention-over-configuration approach, similar maturity, more opinionated
FAQ
Q: How does CodeIgniter 4 compare to Laravel in performance? A: CodeIgniter 4 generally has lower memory usage and faster request times due to its smaller core. Laravel offers more built-in features at the cost of that overhead.
Q: Does CodeIgniter 4 support PHP 8? A: Yes. CodeIgniter 4 requires PHP 8.1 or higher and takes advantage of modern PHP features like enums, fibers, and named arguments.
Q: Is CodeIgniter 4 good for REST APIs? A: Yes. It includes a ResourceController base class and content negotiation for building RESTful services without additional packages.
Q: Can I use Eloquent ORM with CodeIgniter 4? A: Technically possible via Composer, but the framework's built-in Model and Query Builder are designed to work together. Mixing ORMs adds complexity.