@@ -4,6 +4,7 @@ Implementation of JSON API specification for the Yii framework
44[ ![ Latest Stable Version] ( https://poser.pugx.org/tuyakhov/yii2-json-api/v/stable.png )] ( https://packagist.org/packages/tuyakhov/yii2-json-api ) 
55[ ![ Scrutinizer Code Quality] ( https://scrutinizer-ci.com/g/tuyakhov/yii2-json-api/badges/quality-score.png?b=master )] ( https://scrutinizer-ci.com/g/tuyakhov/yii2-json-api/?branch=master )  [ ![ Build Status] ( https://scrutinizer-ci.com/g/tuyakhov/yii2-json-api/badges/build.png?b=master )] ( https://scrutinizer-ci.com/g/tuyakhov/yii2-json-api/build-status/master ) 
66[ ![ Total Downloads] ( https://poser.pugx.org/tuyakhov/yii2-json-api/downloads.png )] ( https://packagist.org/packages/tuyakhov/yii2-json-api ) 
7+ 
78Installation
89------------ 
910
@@ -208,3 +209,124 @@ $model->load(\Yii::$app->request->post());
208209By default type ` users `  will be converted into ` User `  (singular, camelCase) which corresponds to the model's ` formName() `  method (which you may override).
209210You can override the ` JsonApiParser::formNameCallback `  property which refers to a callback that converts 'type' member to form name.
210211Also 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.
212+ 
213+ Examples
214+ -------- 
215+ Controller:
216+ ``` php 
217+ class UserController extends \yii\rest\Controller
218+ {
219+  public $serializer = 'tuyakhov\jsonapi\Serializer';
220+ 
221+  /**
222+  * @inheritdoc
223+  */
224+  public function behaviors()
225+  {
226+  return ArrayHelper::merge(parent::behaviors(), [
227+  'contentNegotiator' => [
228+  'class' => ContentNegotiator::className(),
229+  'formats' => [
230+  'application/vnd.api+json' => Response::FORMAT_JSON,
231+  ],
232+  ]
233+  ]);
234+  }
235+  
236+  /**
237+  * @inheritdoc
238+  */
239+  public function actions()
240+  {
241+  return [
242+  'create' => [
243+  'class' => 'tuyakhov\jsonapi\actions\CreateAction',
244+  'modelClass' => ExampleModel::className()
245+  ],
246+  'update' => [
247+  'class' => 'tuyakhov\jsonapi\actions\UpdateAction',
248+  'modelClass' => ExampleModel::className()
249+  ],
250+  'view' => [
251+  'class' => 'tuyakhov\jsonapi\actions\ViewAction',
252+  'modelClass' => ExampleModel::className(),
253+  ],
254+  'delete' => [
255+  'class' => 'tuyakhov\jsonapi\actions\DeleteAction',
256+  'modelClass' => ExampleModel::className(),
257+  ],
258+  'view-related' => [
259+  'class' => 'tuyakhov\jsonapi\actions\ViewRelatedAction',
260+  'modelClass' => ExampleModel::className()
261+  ],
262+  'update-relationship' => [
263+  'class' => 'tuyakhov\jsonapi\actions\UpdateRelationshipAction',
264+  'modelClass' => ExampleModel::className()
265+  ],
266+  'delete-relationship' => [
267+  'class' => 'tuyakhov\jsonapi\actions\DeleteRelationshipAction',
268+  'modelClass' => ExampleModel::className()
269+  ],
270+  'options' => [
271+  'class' => 'yii\rest\OptionsAction',
272+  ],
273+  ];
274+  }
275+ }
276+ 
277+ ``` 
278+ 
279+ Model:
280+ ``` php 
281+ class User extends ActiveRecord implements LinksInterface, ResourceInterface
282+ {
283+  use ResourceTrait;
284+  
285+  public function getLinks()
286+  {
287+  $reflect = new \ReflectionClass($this);
288+  $controller = Inflector::camel2id($reflect->getShortName());
289+  return [
290+  Link::REL_SELF => Url::to(["$controller/view", 'id' => $this->getId()], true)
291+  ];
292+  }
293+ }
294+ ``` 
295+ 
296+ Configuration file ` config/main.php ` :
297+ ``` php 
298+ return [
299+  // ...
300+  'components' => [
301+  'request' => [
302+  'parsers' => [
303+  'application/vnd.api+json' => 'tuyakhov\jsonapi\JsonApiParser',
304+  ]
305+  ],
306+  'response' => [
307+  'format' => \yii\web\Response::FORMAT_JSON,
308+  'formatters' => [
309+  \yii\web\Response::FORMAT_JSON => 'tuyakhov\jsonapi\JsonApiResponseFormatter'
310+  ]
311+  ],
312+  'urlManager' => [
313+  'rules' => [
314+  [
315+  'class' => 'yii\rest\UrlRule',
316+  'controller' => 'user',
317+  'extraPatterns' => [
318+  'GET {id}/<name: \w+ >' => 'view-related',
319+  'PATCH {id}/relationships/<name: \w+ >' => 'update-relationship',
320+  'DELETE {id}/relationships/<name: \w+ >' => 'delete-relationship',
321+  '{id}/<name: \w+ >' => 'options'
322+  ],
323+  'except' => ['index'],
324+  ],
325+ 
326+  ]
327+  ]
328+  // ...
329+  ]
330+  // ...
331+ ]
332+ ``` 
0 commit comments