@@ -23,49 +23,52 @@ class ApiController extends yidas\rest\Controller
23
23
{
24
24
public function index()
25
25
{
26
- return $this->json(['bar'=>'foo']);
26
+ return $this->response-> json(['bar'=>'foo']);
27
27
}
28
28
}
29
29
```
30
30
31
- Output:
31
+ Output with status ` 200 OK ` :
32
32
33
33
``` json
34
34
{"bar" :" foo" }
35
35
```
36
36
37
- ### Body Formatter
37
+ ### RESTful Create Callback
38
38
39
39
``` 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) {
45
41
42
+ $this->db->insert('mytable', $requestData);
43
+ $id = $this->db->insert_id();
44
+
45
+ return $this->response->json(['id'=>$id], 201);
46
+ }
46
47
```
47
48
48
- Output:
49
+ Output with status ` 201 Created ` :
49
50
50
51
``` json
51
- {"code" : 403 , "message" : " API forbidden " , "data" :{ "bar" : " foo " } }
52
+ {"id" : 1 }
52
53
```
53
54
54
- ### Update Example
55
+ ### Packed Standard Format
55
56
56
57
``` 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());
62
64
}
65
+
63
66
```
64
67
65
- Output:
68
+ Output with status ` 403 Forbidden ` :
66
69
67
70
``` json
68
- {"code" :200 }
71
+ {"code" :403 , "message" : " API forbidden " , "data" :{ "bar" : " foo " } }
69
72
```
70
73
71
74
---
@@ -102,7 +105,7 @@ CONFIGURATION
102
105
1 . Create a controller to extend ` yidas\rest\Controller ` ,
103
106
104
107
``` php
105
- class ApiController extends yidas\rest\Controller {}
108
+ class ResourceController extends yidas\rest\Controller {}
106
109
```
107
110
108
111
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
189
192
class ApiController extends yidas\rest\Controller {
190
193
191
194
protected $routes = [
192
- 'index' => '_list ',
195
+ 'index' => 'find ',
193
196
'store' => 'save',
194
197
'show' => 'display',
195
198
'update' => 'edit',
@@ -198,27 +201,34 @@ class ApiController extends yidas\rest\Controller {
198
201
}
199
202
```
200
203
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 .
202
205
>
203
- > For example: REST list ` index ` action will run ` _list ` method.
206
+ > For example: REST list ` index ` action will run ` find ` method.
204
207
205
208
206
209
### Usage
207
210
208
- #### json()
211
+ #### ` pack() `
209
212
210
- Output by JSON format with optinal body format
213
+ Pack array data into body format
211
214
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.
219
216
220
217
``` 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
+ }
222
232
```
223
233
224
234
---
@@ -230,13 +240,13 @@ The PSR-7 request component `yidas\http\request` is loaded with `yidas\rest\Cont
230
240
231
241
### Usage
232
242
233
- #### getAuthCredentialsWithBasic()
243
+ #### ` getAuthCredentialsWithBasic() `
234
244
235
245
``` php
236
246
list($username, $password) = $this->request->getAuthCredentialsWithBasic();
237
247
```
238
248
239
- #### getAuthCredentialsWithBearer()
249
+ #### ` getAuthCredentialsWithBearer() `
240
250
241
251
``` php
242
252
$b64token = $this->request->getAuthCredentialsWithBearer();
@@ -251,19 +261,27 @@ The PSR-7 response component `yidas\http\response` is loaded with `yidas\rest\Co
251
261
252
262
### Usage
253
263
254
- #### setFormat()
264
+ #### ` json() `
265
+
266
+ JSON output shortcut
267
+
268
+ ``` php
269
+ $this->response->json(['bar'=>'foo'], 201);
270
+ ```
271
+
272
+ #### ` setFormat() `
255
273
256
274
``` php
257
275
$this->response->setFormat(\yidas\http\Response::FORMAT_JSON);
258
276
```
259
277
260
- #### setData()
278
+ #### ` setData() `
261
279
262
280
``` php
263
281
$this->response->setData(['foo'=>'bar']);
264
282
```
265
283
266
- #### send()
284
+ #### ` send() `
267
285
268
286
``` php
269
287
$this->response->send();
0 commit comments