RESTful APIs in CodeIgniter

CodeIgniter 133 views Dec 22, 2025 2 min read

RESTful APIs in CodeIgniter (CodeIgniter 4)

CodeIgniter 4 has first-class support for RESTful APIs, making it easy to build APIs using controllers, routing, validation, and proper HTTP responses.


1. What Is a RESTful API?

REST (Representational State Transfer) uses:

  • HTTP methods (GET, POST, PUT, DELETE)

  • Stateless requests

  • JSON responses

  • Proper HTTP status codes


2. API Folder Structure (Recommended)

app/ └── Controllers/ └── Api/ └── Users.php

3. Create a Resource Controller

php spark make:controller Api/Users --resource

This creates a controller with REST methods:

  • index() → GET

  • show() → GET /id

  • create() → POST

  • update() → PUT/PATCH

  • delete() → DELETE


4. Example API Controller

namespace App\Controllers\Api; use CodeIgniter\RESTful\ResourceController; use App\Models\UserModel; class Users extends ResourceController { protected $modelName = 'App\Models\UserModel'; protected $format = 'json'; public function index() { return $this->respond( $this->model->findAll() ); } public function show($id = null) { $user = $this->model->find($id); if (! $user) { return $this->failNotFound('User not found'); } return $this->respond($user); } }

5. API Routes

In app/Config/Routes.php:

$routes->group('api', function($routes) { $routes->resource('users'); });

Endpoints:

GET /api/users GET /api/users/1 POST /api/users PUT /api/users/1 DELETE /api/users/1

6. Create (POST Request)

public function create() { $data = $this->request->getJSON(true); if (! $this->model->insert($data)) { return $this->failValidationErrors( $this->model->errors() ); } return $this->respondCreated($data); }

7. Update (PUT / PATCH)

public function update($id = null) { $data = $this->request->getJSON(true); if (! $this->model->update($id, $data)) { return $this->failValidationErrors( $this->model->errors() ); } return $this->respondUpdated($data); }

8. Delete

public function delete($id = null) { if (! $this->model->find($id)) { return $this->failNotFound('User not found'); } $this->model->delete($id); return $this->respondDeleted(['id' => $id]); }

9. Validation in API

protected $validationRules = [ 'email' => 'required|valid_email' ];

10. API Authentication Options

  • API Keys

  • JWT (JSON Web Tokens)

  • OAuth 2.0

  • CodeIgniter Shield


11. HTTP Status Codes

CodeMeaning
200OK
201Created
400Bad Request
401Unauthorized
404Not Found
500Server Error

12. Best Practices

✅ Use ResourceController
✅ Always return JSON
✅ Validate request data
✅ Use proper status codes
❌ Don’t expose sensitive data


Summary

  • CodeIgniter supports REST APIs out of the box

  • Resource controllers simplify CRUD APIs

  • Proper routing, validation & responses

  • Secure APIs using authentication

Some advanced sections are available for Registered Members
Share this Post
🚀 Want to Test Your Knowledge?

Take quizzes related to this topic and see where you stand!

Start Quiz Now
Back to Tutorials