Search by

moudarir / codeigniter-rest-api

moudarir

A RESTful server implementation for CodeIgniter 3

Package info

github.com/moudarir/codeigniter-rest-api

pkg:composer/moudarir/codeigniter-rest-api

Statistics

Installs: 100

Dependents: 0

Suggesters: 0

Stars: 7

Open Issues: 0

4.0.0 2026-11-07 00:57 UTC

This package is auto-updated.

Last update: 2026-11-07 01:11:18 UTC


README

GitHub release (latest) GitHub license

A RESTful server implementation for CodeIgniter 3.

Table of contents

  1. Requirements
  2. Installation
  3. Usage
  4. Todo

Requirements

  • PHP 8.4+
  • CodeIgniter 3.1.*.

Installation

This library uses Composer to be installed.

Run this command line (recommended) in the same path as your composer.json file:

composer require moudarir/codeigniter-rest-api

Or, In your composer.json file, add the following code in require section:

{
  ...
  "require": {
    ...
    "moudarir/codeigniter-rest-api": "^4.0"
  },
  ...
}

And then run:

composer update

Configuration

To add on next release.

Important

Execute the dumping/queries.sql file to create the tables needed for the API to work properly.

Tables that will be created are client, client_limit and client_log.

You're now ready to begin using the library 👌.

Usage

Create the [your-project]/application/controllers/api/ApiRequests.php controller:

<?php
defined('BASEPATH') || exit('No direct script access allowed');

use App\Models\Client\Client;
use Moudarir\CIRestApi\ApiController;
use Moudarir\CIRestApi\Exceptions\ApiException;
use Moudarir\CIRestApi\ApiConfig;
use Moudarir\Helpers\Datetime\DatetimeHelper;
use Firebase\JWT\JWT;

/**
 * @property ApiRequests
 */
class ApiRequests extends ApiController
{

    /**
     * @throws ApiException
     */
    public function __construct()
    {
        parent::__construct(ApiConfig::create(
            enableAuthentication: true,
            authenticationClassName: Client::class,
            authenticationJWTSecret: "YOUR_JWT_SECRET",
            authenticationJWTAlgo: "HS256",
            enableApiKey: true,
        ));
    }

    public function token(): void
    {
        $now = DatetimeHelper::now();

        $this->response->ok([
            'data' => JWT::encode(
                [
                    'iss' => base_url(),
                    'iat' => $now->getTimestamp(),
                    'exp' => $now->addMonths()->getTimestamp(),
                    'client_id' => $this->client->getId()
                ],
                $this->apiConfig->authenticationJWTSecret,
                $this->apiConfig->authenticationJWTAlgo
            )
        ]);
    }
}

Authentication methods

The Rest Server can be used with Basic or Bearer authorization type. However, it can be used without any authorization type (not secure).

✨ That's it!

Todo

  • Improve documentation.