Skip to content

Commit

Permalink
Add feature REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
haifahrul committed Sep 22, 2019
1 parent c19dea1 commit e03303e
Show file tree
Hide file tree
Showing 11 changed files with 4,103 additions and 0 deletions.
605 changes: 605 additions & 0 deletions application/config/rest.php

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions application/controllers/Rest_server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Rest_server extends CI_Controller {

public function index()
{
$this->load->helper('url');

$this->load->view('rest_server');
}
}
142 changes: 142 additions & 0 deletions application/controllers/api/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
/** @noinspection PhpIncludeInspection */
require APPPATH . '/libraries/REST_Controller.php';

// use namespace
use Restserver\Libraries\REST_Controller;

/**
* This is an example of a few basic user interaction methods you could use
* all done with a hardcoded array
*
* @package CodeIgniter
* @subpackage Rest Server
* @category Controller
* @author Phil Sturgeon, Chris Kacerguis
* @license MIT
* @link https://github.com/chriskacerguis/codeigniter-restserver
*/
class Example extends REST_Controller {

function __construct()
{
// Construct the parent class
parent::__construct();

// Configure limits on our controller methods
// Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
$this->methods['users_get']['limit'] = 500; // 500 requests per hour per user/key
$this->methods['users_post']['limit'] = 100; // 100 requests per hour per user/key
$this->methods['users_delete']['limit'] = 50; // 50 requests per hour per user/key
}

public function users_get()
{
// Users from a data store e.g. database
$users = [
['id' => 1, 'name' => 'John', 'email' => '[email protected]', 'fact' => 'Loves coding'],
['id' => 2, 'name' => 'Jim', 'email' => '[email protected]', 'fact' => 'Developed on CodeIgniter'],
['id' => 3, 'name' => 'Jane', 'email' => '[email protected]', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
];

$id = $this->get('id');

// If the id parameter doesn't exist return all the users

if ($id === NULL)
{
// Check if the users data store contains users (in case the database result returns NULL)
if ($users)
{
// Set the response and exit
$this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}

// Find and return a single record for a particular user.
else {
$id = (int) $id;

// Validate the id.
if ($id <= 0)
{
// Invalid id, set the response and exit.
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}

// Get the user from the array, using the id as key for retrieval.
// Usually a model is to be used for this.

$user = NULL;

if (!empty($users))
{
foreach ($users as $key => $value)
{
if (isset($value['id']) && $value['id'] === $id)
{
$user = $value;
}
}
}

if (!empty($user))
{
$this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
}
else
{
$this->set_response([
'status' => FALSE,
'message' => 'User could not be found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
}

public function users_post()
{
// $this->some_model->update_user( ... );
$message = [
'id' => 100, // Automatically generated by the model
'name' => $this->post('name'),
'email' => $this->post('email'),
'message' => 'Added a resource'
];

$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
}

public function users_delete()
{
$id = (int) $this->get('id');

// Validate the id.
if ($id <= 0)
{
// Set the response and exit
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}

// $this->some_model->delete_something($id);
$message = [
'id' => $id,
'message' => 'Deleted the resource'
];

$this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
}

}
Loading

0 comments on commit e03303e

Please sign in to comment.