RESTful APIs in CodeIgniter

πŸ“˜ CodeIgniter πŸ‘ 32 views πŸ“… Dec 22, 2025
⏱ Estimated reading time: 2 min

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
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes