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
Copy file name to clipboardExpand all lines: README.md
+34-2Lines changed: 34 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ class Controller extends \yii\rest\Controller
46
46
```
47
47
By default, the value of `type` is automatically pluralized.
48
48
You can change this behavior by setting `tuyakhov\jsonapi\Serializer::$pluralize` property:
49
-
```
49
+
```php
50
50
class Controller extends \yii\rest\Controller
51
51
{
52
52
public $serializer = [
@@ -166,7 +166,7 @@ As the result:
166
166
}
167
167
}
168
168
```
169
-
Enabling JSON Input
169
+
Enabling JSON API Input
170
170
---------------------------
171
171
To let the API accept input data in JSON API format, configure the [[yii\web\Request::$parsers|parsers]] property of the request application component to use the [[tuyakhov\jsonapi\JsonApiParser]] for JSON input
172
172
```php
@@ -176,3 +176,35 @@ To let the API accept input data in JSON API format, configure the [[yii\web\Req
176
176
]
177
177
]
178
178
```
179
+
By default it parses a HTTP request body so that you can populate model attributes with user inputs.
180
+
For example the request body:
181
+
```javascript
182
+
{
183
+
"data": {
184
+
"type":"users",
185
+
"id":"1",
186
+
"attributes": {
187
+
"first-name":"Bob",
188
+
"last-name":"Homster"
189
+
}
190
+
}
191
+
}
192
+
```
193
+
Will be resolved into the following array:
194
+
```php
195
+
// var_dump($_POST);
196
+
[
197
+
"User" => [
198
+
"first_name" => "Bob",
199
+
"last_name" => "Homster"
200
+
]
201
+
]
202
+
```
203
+
So you can access request body by calling `\Yii::$app->request->post()` and simply populate the model with input data:
204
+
```php
205
+
$model = new User();
206
+
$model->load(\Yii::$app->request->post());
207
+
```
208
+
By default type `users` will be converted into `User` (singular, camelCase) which corresponds to the model's `formName()` method (which you may override).
209
+
You can override the `JsonApiParser::formNameCallback` property which refers to a callback that converts 'type' member to form name.
210
+
Also you could change the default behavior for conversion of member names to variable names ('first-name' converts into 'first_name') by setting `JsonApiParser::memberNameCallback` property.
0 commit comments