7
7
8
8
## Table of contents ##
9
9
10
+
10
11
* [ Usage examples] ( #usage-examples )
11
12
* [ Use clause] ( #use-clause )
12
13
* [ Success] ( #success )
17
18
## Usage examples ##
18
19
19
20
The following examples assume ` ResponseBuilder ` is properly installed and available to your Laravel application.
20
- Installation steps are described in details in further chapters, if help is needed .
21
+ See [ Installation and Configuration ] ( docs.md#installation-and-configuration ) for more details.
21
22
22
23
### Use clause ###
23
24
24
- The library is namespaced, so to simplify the use cases, it's recommended to add propr ` use ` at the beginning
25
- of your controller to "import" ` Builder ` class:
26
-
27
- ``` php
28
- use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
29
- ```
25
+ Basic, and mosts common usage examples.
30
26
31
27
#### Success ####
32
28
33
- To report response indicating i.e. operation success, simply your Controller method with:
29
+ To create response indicating operation success, simply conclude your Controller method with:
34
30
35
31
``` php
36
32
return RB::success();
@@ -48,14 +44,15 @@ return RB::success();
48
44
}
49
45
```
50
46
51
- If you would like to return some data with it (which pretty much always the case :), pass it to ` success() ` as argument:
47
+ If you would like to return some data in your response, which is pretty much always the case for success type of responses:), pass
48
+ your payload as ` success() ` 's argument:
52
49
53
50
``` php
54
51
$data = [ 'foo' => 'bar' ];
55
52
return RB::success($data);
56
53
```
57
54
58
- which would return :
55
+ This which would produce :
59
56
60
57
``` json
61
58
{
@@ -69,17 +66,12 @@ return RB::success($data);
69
66
}
70
67
```
71
68
72
- ** NOTE:** As all the data in the response structure must be represented in JSON, ` ResponseBuilder ` only accepts certain types of
73
- data - you can either pass an ` array ` or object of any class that can be converted to valid JSON (i.e. Eloquent's Model or
74
- Collection). Data conversion goes on-the-fly, if you need any additional classes supported than said Model or Collection (which
75
- are pre-configured), you need to instruct ` ResponseBuilder ` how to deal with it. See [ Data Conversion] ( #data-conversion ) chapter
76
- for more details. Attempt to pass unsupported data type (i.e. literals) will throw the exception.
69
+ ** NOTE:** As all the data in the response structure must strictly follow response structure and end up in form os valid JSON data.
70
+ ` ResponseBuilder ` deals with all the primitives and most commonly used classes, using on-the-fly data conversion. You can easily
71
+ add own converters if none of built-in handles your data or fits your needs.See [ Data Conversion] ( #data-conversion ) chapter details.
77
72
78
- ** IMPORTANT:** ` data ` node is ** always** an JSON Object. This is ** enforced** by the library design, therefore if you need to
79
- return your data array as array and just its elements as shown in above example, it will be wrapped into additional array:
80
73
81
74
``` php
82
- // this is CORRECT
83
75
$returned_array = [1,2,3];
84
76
return RB::success($data);
85
77
```
@@ -93,26 +85,6 @@ return RB::success($data);
93
85
"items" : [1 , 2 , 3 ]
94
86
}
95
87
}
96
- ```
97
-
98
- ** IMPORTANT:** If provided array has at least one non numeric index, then wrapping will not occur:
99
-
100
- ``` php
101
- // this is WRONG
102
- $returned_array = ['foo' => 1, 'bar' => 2];
103
- return RB::success($returned_array);
104
- ```
105
-
106
- would give you ` data ` structure:
107
-
108
- ``` json
109
- {
110
- ...
111
- "data" : {
112
- "foo" : 1 ,
113
- "bar" : 2
114
- }
115
- }
116
88
```
117
89
118
90
#### Errors ####
0 commit comments