CodeIgniter
Overview
CodeIgniter 4 is a lightweight PHP MVC framework prized for a small footprint, fast performance, and near-zero configuration. It provides routing, controllers, models with a fluent Query Builder, validation, and the spark CLI without imposing heavy conventions. Read this when building a small-to-mid PHP app or API where you want explicit control and minimal boilerplate.
Official sources
- Docs: https://codeigniter.com/user_guide/
- Repo: https://github.com/codeigniter4/CodeIgniter4
- Install: https://codeigniter.com/user_guide/installation/installing_composer.html
Install / setup
composer create-project codeigniter4/appstarter project-root
cd project-root && php spark serve
Source: https://codeigniter.com/user_guide/installation/installing_composer.html (creates project-root with the latest framework as a dependency; spark serve starts the dev server on :8080).
Core concepts
- MVC structure —
app/Controllers,app/Models,app/Viewshold the application code. - Routing —
app/Config/Routes.phpmaps URLs to controller methods; auto-routing is opt-in. - Controllers — extend
BaseController; return strings, views, or responses. - Models & Entities — extend
CodeIgniter\Modelfor CRUD, validation, and result hydration. - Query Builder — chainable, DB-agnostic query API via
$this->db->table(...). - spark CLI —
php sparkruns migrations, generators, the dev server, and custom commands. - Filters — request/response middleware (auth, CSRF, CORS) configured in
Config\Filters. - Environment config —
.envplusapp/Config/*classes; setCI_ENVIRONMENT.
Best practices
- Disable auto-routing and define explicit routes for security (https://codeigniter.com/user_guide/incoming/routing.html).
- Use
ResourceController/ResourcePresenterfor RESTful endpoints (https://codeigniter.com/user_guide/incoming/restful.html). - Configure the database and base URL via
.env, keep it out of version control (https://codeigniter.com/user_guide/general/configuration.html). - Use the Model layer and validation rather than raw queries for user input (https://codeigniter.com/user_guide/models/model.html).
Common pitfalls
- Leaving auto-routing (legacy) enabled exposes unintended methods → set routing to defined routes only.
- Wrong
baseURLinapp/Config/App.php/.envbreaks asset and redirect URLs → set it correctly per environment. - Forgetting
CI_ENVIRONMENT=productionleaves verbose error output on → set it for prod.
Examples
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
class Users extends ResourceController
{
protected $modelName = \App\Models\UserModel::class;
protected $format = 'json';
public function show($id = null)
{
return $this->respond($this->model->find($id));
}
}
Further reading
- https://codeigniter.com/user_guide/intro/index.html — framework overview and welcome
- https://codeigniter.com/user_guide/database/query_builder.html — Query Builder reference
Related skills
- ../symfony — heavier, component-based PHP framework
- ../nginx — front CodeIgniter with Nginx + PHP-FPM