Overview

Packages

  • CodeIgniter
    • Libraries
    • Rest
  • None

Classes

  • Example
  • Format
  • Key
  • REST_Controller
  • Rest_server
  • Welcome
  • Overview
  • Package
  • Class
  1: <?php
  2: 
  3: defined('BASEPATH') OR exit('No direct script access allowed');
  4: 
  5: // This can be removed if you use __autoload() in config.php OR use Modular Extensions
  6: require APPPATH . '/libraries/REST_Controller.php';
  7: 
  8: /**
  9:  * This is an example of a few basic user interaction methods you could use
 10:  * all done with a hardcoded array
 11:  *
 12:  * @package         CodeIgniter
 13:  * @subpackage      Rest Server
 14:  * @category        Controller
 15:  * @author          Phil Sturgeon, Chris Kacerguis
 16:  * @license         MIT
 17:  * @link            https://github.com/chriskacerguis/codeigniter-restserver
 18:  */
 19: class Example extends REST_Controller {
 20: 
 21:     function __construct()
 22:     {
 23:         // Construct the parent class
 24:         parent::__construct();
 25: 
 26:         // Configure limits on our controller methods
 27:         // Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
 28:         $this->methods['user_get']['limit'] = 500; // 500 requests per hour per user/key
 29:         $this->methods['user_post']['limit'] = 100; // 100 requests per hour per user/key
 30:         $this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
 31:     }
 32: 
 33:     public function users_get()
 34:     {
 35:         // Users from a data store e.g. database
 36:         $users = [
 37:             ['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
 38:             ['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
 39:             ['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
 40:         ];
 41: 
 42:         $id = $this->get('id');
 43: 
 44:         // If the id parameter doesn't exist return all the users
 45: 
 46:         if ($id === NULL)
 47:         {
 48:             // Check if the users data store contains users (in case the database result returns NULL)
 49:             if ($users)
 50:             {
 51:                 // Set the response and exit
 52:                 $this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
 53:             }
 54:             else
 55:             {
 56:                 // Set the response and exit
 57:                 $this->response([
 58:                     'status' => FALSE,
 59:                     'message' => 'No users were found'
 60:                 ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
 61:             }
 62:         }
 63: 
 64:         // Find and return a single record for a particular user.
 65: 
 66:         $id = (int) $id;
 67: 
 68:         // Validate the id.
 69:         if ($id <= 0)
 70:         {
 71:             // Invalid id, set the response and exit.
 72:             $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
 73:         }
 74: 
 75:         // Get the user from the array, using the id as key for retreival.
 76:         // Usually a model is to be used for this.
 77: 
 78:         $user = NULL;
 79: 
 80:         if (!empty($users))
 81:         {
 82:             foreach ($users as $key => $value)
 83:             {
 84:                 if (isset($value['id']) && $value['id'] === $id)
 85:                 {
 86:                     $user = $value;
 87:                 }
 88:             }
 89:         }
 90: 
 91:         if (!empty($user))
 92:         {
 93:             $this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
 94:         }
 95:         else
 96:         {
 97:             $this->set_response([
 98:                 'status' => FALSE,
 99:                 'message' => 'User could not be found'
100:             ], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
101:         }
102:     }
103: 
104:     public function users_post()
105:     {
106:         // $this->some_model->update_user( ... );
107:         $message = [
108:             'id' => 100, // Automatically generated by the model
109:             'name' => $this->post('name'),
110:             'email' => $this->post('email'),
111:             'message' => 'Added a resource'
112:         ];
113: 
114:         $this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
115:     }
116: 
117:     public function users_delete()
118:     {
119:         $id = (int) $this->get('id');
120: 
121:         // Validate the id.
122:         if ($id <= 0)
123:         {
124:             // Set the response and exit
125:             $this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
126:         }
127: 
128:         // $this->some_model->delete_something($id);
129:         $message = [
130:             'id' => $id,
131:             'message' => 'Deleted the resource'
132:         ];
133: 
134:         $this->set_response($message, REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
135:     }
136: 
137: }
138: 
API documentation generated by ApiGen