Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/presttec/codeigniter-restserver
https://github.com/presttec/codeigniter-restserver
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/presttec/codeigniter-restserver
- Owner: presttec
- License: mit
- Created: 2020-06-26T21:27:11.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-26T21:36:52.000Z (over 4 years ago)
- Last Synced: 2024-11-14T14:22:31.550Z (about 2 months ago)
- Language: PHP
- Size: 32.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CodeIgniter RestServer
[![StyleCI](https://github.styleci.io/repos/230589/shield?branch=master)](https://github.styleci.io/repos/230589)
A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.
## Requirements
- PHP 7.2 or greater
- CodeIgniter 3.1.11+## Installation
```sh
composer require presttec/codeigniter-restserver
```## Usage
CodeIgniter Rest Server is available on [Packagist](https://packagist.org/packages/presttec/codeigniter-restserver) (using semantic versioning), and installation via composer is the recommended way to install Codeigniter Rest Server. Just add this line to your `composer.json` file:
```json
"presttec/codeigniter-restserver": "^3.1"
```or run
```sh
composer require presttec/codeigniter-restserver
```Note that you will need to copy `rest.php` to your `config` directory (e.g. `application/config`)
Step 1: Add this to your controller (should be before any of your code)
```php
use RestServer\RestController;
```Step 2: Extend your controller
```php
class Example extends RestController
```## Basic GET example
Here is a basic example. This controller, which should be saved as `Api.php`, can be called in two ways:
* `http://domain/api/users/` will return the list of all users
* `http://domain/api/users/id/1` will only return information about the user with id = 1```php
0, 'name' => 'John', 'email' => '[email protected]'],
['id' => 1, 'name' => 'Jim', 'email' => '[email protected]'],
];$id = $this->get( 'id' );
if ( $id === null )
{
// Check if the users data store contains users
if ( $users )
{
// Set the response and exit
$this->response( $users, 200 );
}
else
{
// Set the response and exit
$this->response( [
'status' => false,
'message' => 'No users were found'
], 404 );
}
}
else
{
if ( array_key_exists( $id, $users ) )
{
$this->response( $users[$id], 200 );
}
else
{
$this->response( [
'status' => false,
'message' => 'No such user found'
], 404 );
}
}
}
}
```