PHPackages                             chalarangelo/codeigniter-rap - 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. [API Development](/categories/api)
4. /
5. chalarangelo/codeigniter-rap

ActiveProject[API Development](/categories/api)

chalarangelo/codeigniter-rap
============================

Simple RESTful API boilerplate for the CodeIgniter framework

v1.0.0(8y ago)6495225[1 issues](https://github.com/Chalarangelo/codeigniter-rap/issues)MITPHPPHP &gt;=5.3.7

Since Jun 28Pushed 8y ago12 watchersCompare

[ Source](https://github.com/Chalarangelo/codeigniter-rap)[ Packagist](https://packagist.org/packages/chalarangelo/codeigniter-rap)[ Docs](https://chalarangelo.github.io/codeigniter-rap.css/)[ RSS](/packages/chalarangelo-codeigniter-rap/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

CodeIgniter RAP
===============

[](#codeigniter-rap)

Simple RESTful API boilerplate for the CodeIgniter framework.

```
composer create-project chalarangelo/codeigniter-rap

```

Introduction
------------

[](#introduction)

The **CodeIgniter RESTful API** is a set of simple, modular helpers that can be easily integrated into your CodeIgniter projects, allowing you to create customizable RESTful APIs that suit all your needs. The package includes helpers for database manipulation, JSON Web Token generation, validation and signing, authentication methods, methods for accessing resources using a URI, as well as some utility methods that will help streamline your workflow.

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

[](#requirements)

- **PHP**: 5.3.7 or greater
- **CodeIgniter**: 3.1.5 or greater

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

[](#installation)

1. Copy and paste all the files from **application** to the corresponding directory in your application.
2. Copy and paste the `config.example.ini` file outside your **application** folder or create yours, based on the example provided. Remember to name your file `config.ini` or rename the sample provided.
3. Create your own secret key and update `config.ini` to be completely secure.
4. (Optional) Copy and paste the `.example.htaccess` file and tweak it to your liking. Remember to rename it to `.htaccess`.
5. (Recommended) Enable HTTPS for your server for maximum security.

Contents
--------

[](#contents)

The API is made up of a set of helpers, each one serving a different purpose. Click on each helper below to read about its functionality.

- [error\_code](#error_code)
- [request](#request)
- [database](#database)
- [jwt](#jwt)
- [auth](#auth)
- [rest\_api](#rest_api)

### error\_code

[](#error_code)

Contains values for various error codes. The codes are mostly matched to HTTP status codes, but some of them might not be perfect matches. The error codes are stored as global variables.

VariableValueDescriptionPROHIBITED405The action is not allowed.BAD\_DATA400The data is malfored or does not match expected input.BAD\_CREDENTIALS403The credentials provided cannot be successfully authorized.UNAUTHORIZED403The user has not the proper authorization to perform this action.NO\_COOKIE409The expected cookie was not found.SUCCESS200The action was successful.### request

[](#request)

Contains a single method for retrieving the body of the current request.

- `get_request_body()`: Retrieves the body of the current request.
    - **returns** the body of the current request

### database

[](#database)

Contains multiple methods for connecting and retrieving information from the database.

- `database_connect()`: Establishes a connection to the database. The connection's parameters (host, username, password and database name) are parsed from the `config.ini` file, residing in the CodeIgniter root folder.
    - **returns** a connection to the database
- `database_query($query, $types, $params, $query_type)`: Uses a prepared statement to query the database, returning an associative array or false, based on the query's results.
    - `$query`: The query to the database, as a string.
    - `$types`: A string that contains one or more characters which specify the types for the corresponding bind variables.
    - `$params`: An array of values that will be passed as parameters to the query. The types of the parameters must match the types specified by `$types`.
    - `$query_type`: (Optional) The type of query that will be executed ("SELECT" (default), "INSERT", "UPDATE", "DELETE"). The execution of the prepared statement will return different kinds of results based on the type specified.
    - **returns** an associative array or false
- `database_no_args_query($query)`:
    - Queries the database, returning an associative array or false, based on the query's results. The query must have no arguments (e.g. useful for retrieving all values from a table).
    - `$query`: The query to the database, as a string.
    - **returns** an associative array or false
- `database_error()`: Returns the last connection error, if any.
    - **returns** error description

### jwt

[](#jwt)

Heavily based on [this implementation](https://github.com/rmcdaniel/angular-codeigniter-seed/blob/master/api/application/helpers/jwt_helper.php), the JWT helper allows for the creation of [JSON Web Tokens](https://jwt.io/). The secret key provided in `config.ini` will be used to sign the token.

- `jwt_encode($payload, $key, $algo)`: Creates a JWT string.
    - `$payload`: The payload of the JSON Web Token.
    - `$key`: The secret key
    - `$algo`: (Optional) The signing algorithm ('HS256' (default), 'HS384' or 'HS512').
    - **returns** a signed JWT
- `jwt_decode($jwt, $key, $verify)`: Decodes a JWT string.
    - `$jwt`: The JSON Web Token.
    - `$key`: The secret key.
    - `$verify`: (Optional) Toggles verification of token on/off.
    - **returns** the payload of the JWT
- `sign($msg, $key, $method)`: Signs a string with a given key and algorithm.
    - `$msg`: The message to sign.
    - `$key`: The secret key.
    - `$algo`: (Optional) The signing algorithm ('HS256' (default), 'HS384' or 'HS512').
    - **returns** encrypted message
- `json_e_encode($input)`: Encodes into a JSON string (with error handling).
    - `$input`: Object to be encoded.
    - **returns** the JSON representation of the object
- `json_e_decode($input)`: Decodes a JSON string (with error handling).)
    - `$input`: JSON string to be decoded.
    - **returns** the object representation of the JSON string
- `urlsafe_base64_encode($input)`: Encodes a string with URL-safe Base64.
    - `$input`: A string to be encoded.
    - **returns** the Base64 encoded string
- `urlsafe_base64_decode($input)`: Decodes a string with URL-safe Base64.
    - `$input`: A Base64 string to be decoded.
    - **returns** a decoded string

### auth

[](#auth)

Contains multiple methods used for authorization, authorization validation and usage with JSON Web Tokens in cookies.

- `authorize($table, $fields, $username_field, $password_field, $id_field, $username_value, $password_value, $service_name, $cookie_name)`: Provides authorized access to the system for a user, based on the provided credentials, using a query to the database. If the authorization is successful, a unique JSON Web Token is generated and stored in a cookie.
    - `$table`: The database table to query.
    - `$fields`: An array of names for the fields to be requested.
    - `$username_field`: The name of the username field.
    - `$password_field`: The name of the password field.
    - `$id_field`: The name of the id field.
    - `$username_value`: The value of the username field.
    - `$password_value`: The value of the password field.
    - `$service_name`: The name of the service.
    - `$cookie_name`: The name of the cookie used to store the authorization token.
    - **returns** an associative array
- `generate_jwt_cookie($username_value, $id_value, $service_name, $cookie_name)`: Generates a unique JSON Web Token from the values provided.
    - `$username_value`: The user's unique username.
    - `$id_value`: The user's unique id.
    - `$service_name`: The name of the service.
    - `$cookie_name`: The name of the cookie used to store the authorization token.
    - **returns** void
- `regenerate_jwt_cookie($service_name, $cookie_name)`: Regenerates a unique JSON Web Token from the values provided. Will return a message if no existing cookie is found.
    - `$service_name`: The name of the service.
    - `$cookie_name`: The name of the cookie used to store the authorization token.
    - **returns** an associative array
- `check_jwt_cookie($service_name, $cookie_name)`: Checks the validity of a unique JSON Web Token.
    - `$service_name`: The name of the service.
    - `$cookie_name`: The name of the cookie used to store the authorization token.
    - **returns** true if the cookie is found and the JWT is valid, false otherwise
- `get_jwt_data($cookie_name)`: Gets the data stored in a unique JSON Web Token.
    - `$cookie_name`: The name of the cookie used to store the authorization token.
    - **returns** an associative array

### rest-api

[](#rest-api)

Contains multiple methods, implementing generic CRUD methods for a RESTful API. For security reasons, certain methods are not implemented, but rather return an associative array with an error code and a message.

- `createResourceRoot()`: Creates a new collection, using a query to the database.
    - **returns** an associative array with an error message
- `createResourceElement($table, $input_fields, $input_types, $input_values)`: Creates a new entry in a collection, using a query to the database.
    - `$table`: The database table to query.
    - `$input_fields`: An array of names for the fields to be filled.
    - `$input_types`: A string that contains one or more characters
    - which specify the types for the corresponding fields.
    - `$input_values`: An array of values for the fields to be filled.
    - **returns** the id of the newly-created collection member
- `readResourceRoot($table, $fields)`: Lists the members of a collection, using a query to the database.
    - `$table`: The database table to query.
    - `$fields`: The table's fields to be retrieved.
    - **returns** a collection of elements
- `readResourceElement($table, $fields, $element_key, $key_value)`: Retrieves a specific member of a collection, using a query to the database.
    - `$table`: The database table to query.
    - `$fields`: The table's fields to be retrieved.
    - `$element_key`: The table's field that will be used for the specific
    - resource's indentification.
    - `$key_value`: The value to be used for the specific resource's
    - identification.
    - **returns** a list of members or false
- `updateResourceRoot()`: Updates a resource collection, using a query to the database.
    - **returns** an associative array with an error message
- `updateResourceElement($table, $input_fields, $input_types, $input_values, $element_key, $key_value)`: Updates a specific member of a collection, using a query to the database.
    - `$table`: The database table to query.
    - `$input_fields`: An array of names for the fields to be updated.
    - `$input_types`: A string that contains one or more characters
    - which specify the types for the corresponding fields.
    - `$input_values`: An array of values for the fields to be updated.
    - `$element_key`: The table's field that will be used for the specific
    - resource's indentification.
    - `$key_value`: The value to be used for the specific resource's
    - identification.
    - **returns** the number of affected rows
- `deleteResourceRoot()`: Deletes a resource collection, using a query to the database.
    - **returns** an associative array with an error message
- `deleteResourceElement($table, $element_key, $key_value)`: Deletes a specific member of a collection, using a query to the database.
    - `$table`: The database table to query.
    - `$element_key`: The table's field that will be used for the specific
    - resource's indentification.
    - `$key_value`: The value to be used for the specific resource's
    - identification.
    - **returns** the number of affected rows

How to use
----------

[](#how-to-use)

The provided helpers are supposed to be used in a `CI_Controller`, but you can use them any way you like. The sample provided (**controllers/Api.php**) is a pretty good starting point for a RESTful API implementation:

- All helpers are loaded in the `__construct()` method of the `Api` class. Certain variables are also instantiated to be used for the API's requests.
- The `index()` method is an empty method returning an error message, when no resource is specified in the request in the form of a URI.
- The `users($param)` method maps the different API methods to HTTP methods (CREATE = POST, READ = GET, UPDATE = PUT, DELETE = DELETE) and uses the various helpers and class variables to provide a sample RESTful API implementation.
- The `login($param)` method allows for the authorization of a user (needed to update or delete a resource matched to his/her username).

To query the API, you should use something like `example.com/index.php/api/resource_name` (or `example.com/api/resource_name`, if you are using the `.htaccess` file provided, configured to your environment), replacing `resource_name` with your resource's name (e.g. `users`). Bear in mind that certain requests will return errors due to implementation specifics.

License
-------

[](#license)

The project is licensed under the MIT license.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3243d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/57b4ca70e99ccd46afec726b257e51d5417954209fcd10409d14c65d85273caa?d=identicon)[Chalarangelo](/maintainers/Chalarangelo)

---

Top Contributors

[![Chalarangelo](https://avatars.githubusercontent.com/u/8281875?v=4)](https://github.com/Chalarangelo "Chalarangelo (18 commits)")

### Embed Badge

![Health badge](/badges/chalarangelo-codeigniter-rap/health.svg)

```
[![Health](https://phpackages.com/badges/chalarangelo-codeigniter-rap/health.svg)](https://phpackages.com/packages/chalarangelo-codeigniter-rap)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
