Skip to content

Commit 120b914

Browse files
committed
New response standard with pack feature
1 parent b52adc6 commit 120b914

File tree

3 files changed

+100
-50
lines changed

3 files changed

+100
-50
lines changed

README.md

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,49 +23,52 @@ class ApiController extends yidas\rest\Controller
2323
{
2424
public function index()
2525
{
26-
return $this->json(['bar'=>'foo']);
26+
return $this->response->json(['bar'=>'foo']);
2727
}
2828
}
2929
```
3030

31-
Output:
31+
Output with status `200 OK`:
3232

3333
```json
3434
{"bar":"foo"}
3535
```
3636

37-
### Body Formatter
37+
### RESTful Create Callback
3838

3939
```php
40-
try {
41-
throw new Exception("API forbidden", 403);
42-
} catch (\Exception $e) {
43-
return $this->json(['bar'=>'foo'], true, $e->getCode(), $e->getMessage());
44-
}
40+
public function store($resourceID, $requestData=null) {
4541

42+
$this->db->insert('mytable', $requestData);
43+
$id = $this->db->insert_id();
44+
45+
return $this->response->json(['id'=>$id], 201);
46+
}
4647
```
4748

48-
Output:
49+
Output with status `201 Created`:
4950

5051
```json
51-
{"code":403,"message":"API forbidden","data":{"bar":"foo"}}
52+
{"id":1}
5253
```
5354

54-
### Update Example
55+
### Packed Standard Format
5556

5657
```php
57-
public function update($resourceID, $requestData=null) {
58-
59-
$this->db->where('id', $resourceID)
60-
->update('table', $requestData);
61-
return $this->json(false, true);
58+
try {
59+
throw new Exception("API forbidden", 403);
60+
} catch (\Exception $e) {
61+
// Pack data into a standard format
62+
$data = $this->pack(['bar'=>'foo'], $e->getCode(), $e->getMessage());
63+
return $this->response->json($data, $e->getCode());
6264
}
65+
6366
```
6467

65-
Output:
68+
Output with status `403 Forbidden`:
6669

6770
```json
68-
{"code":200}
71+
{"code":403,"message":"API forbidden","data":{"bar":"foo"}}
6972
```
7073

7174
---
@@ -102,7 +105,7 @@ CONFIGURATION
102105
1. Create a controller to extend `yidas\rest\Controller`,
103106

104107
```php
105-
class ApiController extends yidas\rest\Controller {}
108+
class ResourceController extends yidas\rest\Controller {}
106109
```
107110

108111
2. Add and implement action methods referring by [Build Methods](#build-methods).
@@ -189,7 +192,7 @@ You could override to defind your own routing while creating a resource controll
189192
class ApiController extends yidas\rest\Controller {
190193

191194
protected $routes = [
192-
       'index' => '_list',
195+
       'index' => 'find',
193196
'store' => 'save',
194197
'show' => 'display',
195198
'update' => 'edit',
@@ -198,27 +201,34 @@ class ApiController extends yidas\rest\Controller {
198201
}
199202
```
200203

201-
> The keys are refered to Action of Resource Controller table.
204+
> The keys are refered to Action of Resource Controller table, you must define all routes you need.
202205
>
203-
> For example: REST list `index` action will run `_list` method.
206+
> For example: REST list `index` action will run `find` method.
204207
205208

206209
### Usage
207210

208-
#### json()
211+
#### `pack()`
209212

210-
Output by JSON format with optinal body format
213+
Pack array data into body format
211214

212-
|Item|Type|Description|
213-
|-|-|-|
214-
|param|array\|mixed |Callback data body|
215-
|param| bool |Enable body format|
216-
|param| int |Callback status code|
217-
|param| string |Callback status text|
218-
|return |string| Response body data|
215+
You could override this method for your application standard.
219216

220217
```php
221-
return $this->json(["bar"=>"foo"], true);
218+
$data = $this->pack(['bar'=>'foo'], 403, 'Forbidden');
219+
return $this->response->json($data, 403);
220+
```
221+
222+
JSON Result:
223+
224+
```
225+
{
226+
"code": 403,
227+
"message": "Forbidden",
228+
"data": {
229+
"bar": "foo"
230+
}
231+
}
222232
```
223233

224234
---
@@ -230,13 +240,13 @@ The PSR-7 request component `yidas\http\request` is loaded with `yidas\rest\Cont
230240

231241
### Usage
232242

233-
#### getAuthCredentialsWithBasic()
243+
#### `getAuthCredentialsWithBasic()`
234244

235245
```php
236246
list($username, $password) = $this->request->getAuthCredentialsWithBasic();
237247
```
238248

239-
#### getAuthCredentialsWithBearer()
249+
#### `getAuthCredentialsWithBearer()`
240250

241251
```php
242252
$b64token = $this->request->getAuthCredentialsWithBearer();
@@ -251,19 +261,27 @@ The PSR-7 response component `yidas\http\response` is loaded with `yidas\rest\Co
251261

252262
### Usage
253263

254-
#### setFormat()
264+
#### `json()`
265+
266+
JSON output shortcut
267+
268+
```php
269+
$this->response->json(['bar'=>'foo'], 201);
270+
```
271+
272+
#### `setFormat()`
255273

256274
```php
257275
$this->response->setFormat(\yidas\http\Response::FORMAT_JSON);
258276
```
259277

260-
#### setData()
278+
#### `setData()`
261279

262280
```php
263281
$this->response->setData(['foo'=>'bar']);
264282
```
265283

266-
#### send()
284+
#### `send()`
267285

268286
```php
269287
$this->response->send();

src/http/Response.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,12 @@ public function json($data, $statusCode=null)
225225
$this->setStatusCode($statusCode);
226226
}
227227

228-
return $this->setFormat(Response::FORMAT_JSON)
229-
->setData($data)
230-
->send();
228+
$this->setFormat(Response::FORMAT_JSON);
229+
230+
if (!is_null($data)) {
231+
$this->setData($data);
232+
}
233+
234+
return $this->send();
231235
}
232236
}

src/rest/Controller.php

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* RESTful API Controller
1010
*
1111
* @author Nick Tsai <myintaer@gmail.com>
12-
* @version 1.2.0
12+
* @version 1.3.0
1313
* @link https://github.com/yidas/codeigniter-rest/
1414
* @see https://github.com/yidas/codeigniter-rest/blob/master/examples/RestController.php
1515
*
@@ -26,15 +26,6 @@
2626
*/
2727
class Controller extends \CI_Controller
2828
{
29-
/**
30-
* @var array Standard format
31-
*/
32-
protected $responseFormat = [
33-
'status_code' => 'code',
34-
'message' => 'message',
35-
'body' => 'data',
36-
];
37-
3829
/**
3930
* RESTful API resource routes
4031
*
@@ -144,6 +135,7 @@ public function ajax($resourceID=NULL)
144135
/**
145136
* Output by JSON format with optinal body format
146137
*
138+
* @deprecated 1.3.0
147139
* @param array|mixed Callback data body, false will remove body key
148140
* @param bool Enable body format
149141
* @param int HTTP Status Code
@@ -172,6 +164,7 @@ protected function json($data=[], $bodyFormat=null, $statusCode=null, $message=n
172164
/**
173165
* Format Response Data
174166
*
167+
* @deprecated 1.3.0
175168
* @param int Callback status code
176169
* @param string Callback status text
177170
* @param array|mixed|bool Callback data body, false will remove body key
@@ -195,6 +188,41 @@ protected function _format($statusCode=null, $message=null, $body=false)
195188
return $format;
196189
}
197190

191+
/**
192+
* Pack array data into body format
193+
*
194+
* You could override this method for your application standard
195+
*
196+
* @param array|string $data Original data
197+
* @param int HTTP Status Code
198+
* @param string Callback message
199+
* @return array Packed data
200+
* @example
201+
* $packedData = pack(['bar'=>'foo], 401, 'Login Required');
202+
*/
203+
protected function pack($data, $statusCode=200, $message=null)
204+
{
205+
$packBody = [];
206+
207+
// Status Code
208+
if ($statusCode) {
209+
210+
$packBody['code'] = $statusCode;
211+
}
212+
// Message
213+
if ($message) {
214+
215+
$packBody['message'] = $message;
216+
}
217+
// Data
218+
if (is_array($data) || is_string($data)) {
219+
220+
$packBody['data'] = $data;
221+
}
222+
223+
return $packBody;
224+
}
225+
198226
/**
199227
* Default Action
200228
*/

0 commit comments

Comments
 (0)