|
| 1 | + |
| 2 | +Implementation of JSON API specification for the Yii framework |
| 3 | +================================================================== |
| 4 | +Installation |
| 5 | +------------ |
| 6 | + |
| 7 | +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). |
| 8 | + |
| 9 | +Either run |
| 10 | + |
| 11 | +``` |
| 12 | +php composer.phar require --prefer-dist tuyakhov/yii2-youtube "dev-master" |
| 13 | +``` |
| 14 | + |
| 15 | +or add |
| 16 | + |
| 17 | +``` |
| 18 | +"tuyakhov/yii2-json-api": "dev-master" |
| 19 | +``` |
| 20 | + |
| 21 | +to the require section of your `composer.json` file. |
| 22 | + |
| 23 | + |
| 24 | +Usage |
| 25 | +============================== |
| 26 | +This implementation is not complete yet. Yii2 JSON Api extension is still under hard development process. [Content Negotiation](http://jsonapi.org/format/#content-negotiation) and [Document Structure](http://jsonapi.org/format/#document-structure) are only covered at the moment.<br/> |
| 27 | +Once the extension is installed, simply use it in your code by : |
| 28 | +Data Serializing and Content Negotiation: |
| 29 | +------------------------------------------- |
| 30 | +Controller: |
| 31 | +```php |
| 32 | +class Controller extends \yii\rest\Controller |
| 33 | +{ |
| 34 | + public $serializer = 'tuyakhov\jsonapi\Serializer'; |
| 35 | + |
| 36 | + public function behaviors() |
| 37 | + { |
| 38 | + return ArrayHelper::merge(parent::behaviors(), [ |
| 39 | + 'contentNegotiator' => [ |
| 40 | + 'class' => ContentNegotiator::className(), |
| 41 | + 'formats' => [ |
| 42 | + 'application/vnd.api+json' => Response::FORMAT_JSON, |
| 43 | + ], |
| 44 | + ] |
| 45 | + ]); |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | +Model: |
| 50 | +```php |
| 51 | +use tuyakhov\jsonapi\ResourceTrait; |
| 52 | + |
| 53 | +class User extends ActiveRecord |
| 54 | +{ |
| 55 | + use ResourceTrait; |
| 56 | +} |
| 57 | +``` |
| 58 | +Enabling JSON Input |
| 59 | +--------------------------- |
| 60 | +To let the API accept input data in JSON format, configure the [[yii\web\Request::$parsers|parsers]] property of the request application component to use the [[tuyakhov\jsonapi\JsonParser]] for JSON input |
| 61 | +```php |
| 62 | +'request' => [ |
| 63 | + 'parsers' => [ |
| 64 | + 'application/vnd.api+json' => 'tuyakhov\jsonapi\JsonApiParser', |
| 65 | + ] |
| 66 | +] |
| 67 | +``` |
0 commit comments