PHPackages                             moudarir/codeigniter-rest-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. moudarir/codeigniter-rest-api

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

moudarir/codeigniter-rest-api
=============================

Codeigniter 3 Rest API

3.0.0(2y ago)7981MITPHPPHP &gt;=7.4

Since Feb 8Pushed 2y ago2 watchersCompare

[ Source](https://github.com/moudarir/codeigniter-rest-api)[ Packagist](https://packagist.org/packages/moudarir/codeigniter-rest-api)[ RSS](/packages/moudarir-codeigniter-rest-api/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

Codeigniter 3 API Rest
======================

[](#codeigniter-3-api-rest)

[![GitHub release (latest)](https://camo.githubusercontent.com/e26e3742ccc855e836286d28ce2d937a66bc0d41ae8e63cad0b8810458630090/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6d6f7564617269722f636f646569676e697465722d726573742d617069)](https://packagist.org/packages/moudarir/codeigniter-rest-api)[![GitHub license](https://camo.githubusercontent.com/5d7283a40a25e1824f0145bfda08afa6adb5e00ba6d6f5e4babe3a84ce15770a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6f7564617269722f636f646569676e697465722d726573742d617069)](https://github.com/moudarir/codeigniter-rest-api/blob/master/LICENSE)

A RESTful server implementation for Codeigniter 3 based on [CodeIgniter RestServer](https://github.com/chriskacerguis/codeigniter-restserver "CodeIgniter RestServer")

> **Break changes** made in version `3.0`. Full code refactoring and new route system implemented inspired from [Luthier CI](https://github.com/ingeniasoftware/luthier-ci "Luthier CI").

Table of contents
-----------------

[](#table-of-contents)

1. [Requirements](#requirements)
2. [Installation](#installation)
3. [Implementation](#implementation)
4. [Usage](#usage)
5. [Postman collection](#postman-collection)
6. [Todo](#todo)

Requirements
------------

[](#requirements)

- PHP: `7.4` to `8.2` tested.
- Codeigniter: `^3.1.13`.
- Composer

Installation
------------

[](#installation)

> The current version `3.0.*` requires php 7.4 or higher ([php supported versions](http://php.net/supported-versions.php))

This library uses [Composer](https://getcomposer.org/) 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": "^3.0"
  },
  ...
}
```

And then run:

```
composer install
```

Implementation
--------------

[](#implementation)

### Language / Translation

[](#language--translation)

You can find the file associated with your language in the `application/language/` folder. Based on the `$config['language']` setting in your `[your-project]/application/config/config.php` configuration file.

### Supported languages

[](#supported-languages)

- English
- French
- Arabic

### Files &amp; Configuration

[](#files--configuration)

The first thing to do is copying all required files in your CI project:

- `application/config/rest-api-server.php` =&gt; `[your-project]/application/config/rest-api-server.php`
- `application/controllers/DefaultController.php` =&gt; `[your-project]/application/controllers/DefaultController.php`
- `application/language/*/rest-api-server_lang.php` =&gt; `[your-project]/application/language/*/rest-api-server_lang.php`
- `application/routes/*` =&gt; `[your-project]/application/routes/*`

> **DO NOT** change the `[your-project]/application/config/rest-api-server.php` and `[your-project]/application/language/*/rest-api-server_lang.php` filenames.

Make sure that the `enable_hooks` and `composer_autoload` keys in `[your-project]/application/config/config.php` file are set as following:

```
$config['enable_hooks'] = true;
$config['composer_autoload'] = true; // Or the path to 'autoload.php' file. Ex: APPPATH.'vendor/autoload.php'
```

Next, set the following code in `[your-project]/application/config/hooks.php` file:

```
$hook = \Moudarir\CodeigniterApi\Routes\Hook::initialize();
```

and in `[your-project]/application/config/routes.php` file:

```
$route = \Moudarir\CodeigniterApi\Routes\Router::getRoutes();
```

### Important

[](#important)

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

Tables that will be created are `users`, `api_keys`, `api_key_limits` and `api_key_logs`.

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

### About Routes

[](#about-routes)

The implementation of old routes is deprecated. The routes are now simplified for best use. See [Usage](#usage).

Usage
-----

[](#usage)

Adding some routes for the next example in `[your-project]/application/routes/api.php` file (if not exists).

```
\Moudarir\CodeigniterApi\Routes\Router::group('users', ['namespace' => 'api'], function () {
    \Moudarir\CodeigniterApi\Routes\Router::get('', 'apiUsers@index');
    \Moudarir\CodeigniterApi\Routes\Router::post('', 'apiUsers@create');
    \Moudarir\CodeigniterApi\Routes\Router::post('login', 'apiUsers@login');
    \Moudarir\CodeigniterApi\Routes\Router::put('{id}', 'apiUsers@update');
    \Moudarir\CodeigniterApi\Routes\Router::get('{id}', 'apiUsers@show');
});

// This will generate route array like this:
/**
$route = [
  "users" => [
    "GET" => "api/apiUsers/index",
    "POST" => "api/apiUsers/create",
  ],
  "users/login" => [
    "POST" => "api/apiUsers/login"
  ],
  "users/([0-9]+)" => [
    "PUT" => "api/apiUsers/update/$1"
    "GET" => "api/apiUsers/show/$1"
  ],
  "default_controller" => "welcome", // Can be changed in '[your-project]/application/config/rest-api-server.php' file.
  "translate_uri_dashes" => false, // Can be changed in '[your-project]/application/config/rest-api-server.php' file.
  "404_override" => "pageNotFound/index", // Can be changed in '[your-project]/application/config/rest-api-server.php' file.
]
**/
```

And now, we can create our `[your-project]/application/controllers/api/ApiUsers.php` controller:

```
