In modern web and mobile development, RESTful APIs play a crucial role in managing data and powering client-server communication. CodeIgniter 4 provides a clean and efficient way to build RESTful APIs using Resource Routes and Controllers. These tools help streamline your CRUD operations by automatically mapping HTTP verbs to specific controller methods, reducing repetitive code and keeping your project organized.
In this tutorial, we’ll walk through how to create, configure, and use resource routes and controllers in CodeIgniter 4 with a complete, practical example.
Let’s get started.
⚪ What is a Resource Controller?
A Resource Controller in CodeIgniter 4 is a controller type designed to handle a standard set of actions for working with resources.
These include:
- index() – List all records
- show($id) – Display a single record
- create() – Show a form to create a new record (if using forms)
- store() – Handle data saving for new records
- edit($id) – Show a form to edit a record
- update($id) – Handle updating an existing record
- delete($id) – Handle record deletion
CodeIgniter automatically maps HTTP verbs (GET, POST, PUT, DELETE) to these methods.
⚪ Creating a Resource Controller
Use the Spark CLI to quickly generate a Resource Controller:
php spark make:controller Product --resource
This will create a Product.php controller inside app/Controllers/ with all the standard resource methods.
⚪ Defining Resource Routes
In app/Config/Routes.php, we can define a Resource Route like this:
$routes->resource('product');
This single line registers all the necessary routes for CRUD operations for the Product resource.
Generated Endpoints:
GET /product
→index()
GET /product/new
→new()
POST /product
→create()
GET /product/{id}
→show($id)
GET /product/{id}/edit
→edit($id)
PUT /product/{id}
→update($id)
DELETE /product/{id}
→delete($id)
⚪ Example Usage (Student CRUD Operation)
Let’s do a complete CRUD operation with Student’s Controller use case.
📍 Command to create resource controller,
php spark make:controller Student --resource
This will create a Student.php
controller inside app/Controllers/
with all the standard resource methods.
📍 Resource Routes
$routes->resource('student');
This single line registers all the necessary routes for CRUD operations for the Student
resource.
📍 Generated Endpoints:
GET /student
→index()
GET /student/new
→new()
POST /student
→create()
GET /student/{id}
→show($id)
GET /student/{id}/edit
→edit($id)
PUT /student/{id}
→update($id)
DELETE /student/{id}
→delete($id)
📍 Student.php Controller class with CRUD functions,
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
class Student extends ResourceController
{
protected $format = 'json';
public function index()
{
return $this->respond(['message' => 'List of all students']);
}
public function show($id = null)
{
return $this->respond(['message' => 'Details for student ID ' . $id]);
}
public function create()
{
return $this->respondCreated(['message' => 'Student created successfully']);
}
public function update($id = null)
{
return $this->respond(['message' => 'Student with ID ' . $id . ' updated']);
}
public function delete($id = null)
{
return $this->respondDeleted(['message' => 'Student with ID ' . $id . ' deleted']);
}
}
⚪ Conclusion
CodeIgniter 4’s Resource Controllers and Routes provide a clean, efficient way to build RESTful APIs with minimal code. By generating routes automatically and following REST conventions, you can keep your application scalable, organized, and easy to maintain.
Read more