You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 14, 2023. It is now read-only.
*[Using in Laravel 5+ framework](#using-in-laravel-5-framework)
39
+
*[Copyright and License](#copyright-and-license)
40
+
41
+
20
42
## Installation
21
43
22
44
To get the latest version of `API Response`, simply require the project using [Composer](https://getcomposer.org/):
@@ -51,44 +73,44 @@ Alright! Use `api_response()` helper.
51
73
52
74
## Using
53
75
54
-
### returned NULL with code:
76
+
### as NULL with code:
55
77
```php
56
78
return api_response(null, 304);
57
79
```
58
-
returned with code 304:
80
+
return with code 304:
59
81
```json
60
82
{
61
83
"data": null
62
84
}
63
85
```
64
86
65
-
### returned integer with default code:
87
+
### as integer with default code:
66
88
```php
67
89
return api_response(304);
68
90
```
69
-
returned with code 200:
91
+
return with code 200:
70
92
```json
71
93
{
72
94
"data": 304
73
95
}
74
96
```
75
97
76
-
### returned string with default code:
98
+
### as string with default code:
77
99
```php
78
100
return api_response('qwerty');
79
101
```
80
-
returned with code 200:
102
+
return with code 200:
81
103
```json
82
104
{
83
105
"data": "qwerty"
84
106
}
85
107
```
86
108
87
-
### returned string with code:
109
+
### as string with code:
88
110
```php
89
111
return api_response('qwerty', 400);
90
112
```
91
-
returned with code 400:
113
+
return with code 400:
92
114
```json
93
115
{
94
116
"error": {
@@ -98,11 +120,11 @@ returned with code 400:
98
120
}
99
121
```
100
122
101
-
### returned integer with code:
123
+
### as integer with code:
102
124
```php
103
125
return api_response(304, 400);
104
126
```
105
-
returned with code 400:
127
+
return with code 400:
106
128
```json
107
129
{
108
130
"error": {
@@ -112,7 +134,7 @@ returned with code 400:
112
134
}
113
135
```
114
136
115
-
### returned array:
137
+
### as array:
116
138
```php
117
139
$data = [
118
140
[
@@ -130,7 +152,7 @@ $data = [
130
152
```php
131
153
return api_response($data, 400);
132
154
```
133
-
returned with code 400:
155
+
return with code 400:
134
156
```json
135
157
{
136
158
"error": {
@@ -153,7 +175,7 @@ returned with code 400:
153
175
```php
154
176
return api_response($data, 200);
155
177
```
156
-
returned with code 200:
178
+
return with code 200:
157
179
```json
158
180
{
159
181
"data": [
@@ -169,22 +191,199 @@ returned with code 200:
169
191
}
170
192
```
171
193
172
-
If the first parameter is a number, then the decryption of the error by code will be returned. In other cases, the value of the passed variable will be returned.
194
+
If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.
If you do not want to wrap the response in the `data` key, you need to write your helper that calls the service or use `Helldar\ApiResponse\Services\Response` class:
247
+
248
+
```php
249
+
use Helldar\ApiResponse\Services\Response;
250
+
251
+
function api_response($data = null, int $status_code = 200, array $headers = [], array $with = [])
252
+
{
253
+
return Response::init()
254
+
->headers($headers)
255
+
->data($data, false)
256
+
->with($with)
257
+
->status($status_code)
258
+
->response();
259
+
}
260
+
```
261
+
262
+
### as NULL with code and without `data` key:
263
+
```php
264
+
return api_response(null, 304);
265
+
```
266
+
return with code 304:
267
+
```json
268
+
{}
269
+
```
270
+
271
+
### as integer with default code and without `data` key:
272
+
```php
273
+
return api_response(304);
274
+
```
275
+
return with code 200:
276
+
```json
277
+
304
278
+
```
279
+
280
+
### as string with default code and without `data` key:
281
+
```php
282
+
return api_response('qwerty');
283
+
```
284
+
return with code 200:
285
+
```json
286
+
"qwerty"
287
+
```
288
+
289
+
### as string with code and without `data` key:
290
+
```php
291
+
return api_response('qwerty', 400);
292
+
```
293
+
return with code 400:
294
+
```json
295
+
{
296
+
"error": {
297
+
"code": 400,
298
+
"data": "qwerty"
299
+
}
300
+
}
301
+
```
302
+
303
+
### as integer with code and without `data` key:
304
+
```php
305
+
return api_response(304, 400);
306
+
```
307
+
return with code 400:
308
+
```json
309
+
{
310
+
"error": {
311
+
"code": 400,
312
+
"data": 304
313
+
}
314
+
}
315
+
```
173
316
317
+
### as array and without `data` key:
318
+
```php
319
+
$data = [
320
+
[
321
+
'title' => 'Title #1',
322
+
'description' => 'Description #1',
323
+
],
324
+
[
325
+
'title' => 'Title #2',
326
+
'description' => 'Description #2',
327
+
],
328
+
];
329
+
```
174
330
175
-
## with additional content
331
+
#### as error and without `data` key
332
+
```php
333
+
return api_response($data, 400);
334
+
```
335
+
return with code 400:
336
+
```json
337
+
{
338
+
"error": {
339
+
"code": 400,
340
+
"data": [
341
+
{
342
+
"title": "Title #1",
343
+
"description": "Description #1"
344
+
},
345
+
{
346
+
"title": "Title #2",
347
+
"description": "Description #2"
348
+
}
349
+
]
350
+
}
351
+
}
352
+
```
353
+
354
+
#### as success and without `data` key
355
+
```php
356
+
return api_response($data, 200);
357
+
```
358
+
return with code 200:
359
+
```json
360
+
[
361
+
{
362
+
"title": "Title #1",
363
+
"description": "Description #1"
364
+
},
365
+
{
366
+
"title": "Title #2",
367
+
"description": "Description #2"
368
+
}
369
+
]
370
+
```
371
+
372
+
If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.
373
+
374
+
### with additional content and without `data` key:
0 commit comments