CodeIgniter 3 RESTful API Controller
class ApiController extends yidas\rest\Controller
{
public function index()
{
return $this->json(['bar'=>'foo']);
}
}
Output:
{"bar":"foo"}
try {
throw new Exception("API forbidden", 403);
} catch (\Exception $e) {
return $this->json(['bar'=>'foo'], true, $e->getCode(), $e->getMessage());
}
Output:
{"code":403,"message":"API forbidden","data":{"bar":"foo"}}
public function update($resourceID, $requestData=null) {
$this->db->where('id', $resourceID)
->update('table', $requestData);
return $this->json(null, true);
}
This library requires the following:
- PHP 5.4.0+
- CodeIgniter 3.0.0+
Run Composer in your Codeigniter project under the folder \application
:
composer require yidas/codeigniter-rest
Check Codeigniter application/config/config.php
:
$config['composer_autoload'] = TRUE;
You could customize the vendor path into
$config['composer_autoload']
- Create a controller to extend
yidas\rest\Controller
,
class ApiController extends yidas\rest\Controller {}
- Add a pair of routes for this controller into
\application\config\routes.php
to enable RESTful API methods:
$route['resource_name'] = '[Controller]/route';
$route['resource_name/(:num)'] = '[Controller]/route/$1';
You could skip this route setting if you just use
index
method of the controller.
The base RESTful API controller is yidas\rest\Controller
, the following table is the actions handled by resource controller, the action
is the CI_Controller
's action name which you could override to open it:
HTTP Method | URI | Action | Description |
---|---|---|---|
GET | /photos | index | List the collection's members. |
POST | /photos | store | Create a new entry in the collection. |
GET | /photos/{photo} | show | Retrieve an addressed member of the collection. |
PUT/PATCH | /photos/{photo} | update | Update the addressed member of the collection. |
DELETE | /photos/{photo} | delete | Delete the addressed member of the collection. |
DELETE | /photos | deleteAll | Delete the entire collection. |
The following methods with arguments could be overrided when you need to defind response and open it:
public function index() {}
public function store($requestData=null) {}
public function show($resourceID) {}
public function update($resourceID, $requestData=null) {}
public function delete($resourceID) {}
public function deleteAll() {}
$requestData
is the raw body from request
$resourceID
is the addressed identity of the resource from request
Output by JSON format with optinal body format
Item | Type | Description |
---|---|---|
param | array|mixed | Callback data body |
param | bool | Enable body format |
param | int | Callback status code |
param | string | Callback status text |
return | string | Response body data |
return $this->json(["bar"=>"foo"], true);
The PSR-7 request component yidas\http\request
is loaded with yidas\rest\Controller
, which provides input handler and HTTP Authentication.
list($username, $password) = $this->request->getAuthCredentialsWithBasic();
$b64token = $this->request->getAuthCredentialsWithBearer();
The PSR-7 response component yidas\http\response
is loaded with yidas\rest\Controller
, which provides output handler and formatter.
$this->response->setFormat(\yidas\http\Response::FORMAT_JSON);
$this->response->setData(['foo'=>'bar']);
$this->response->send();