The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist tuyakhov/yii2-json-api "*" or add
"tuyakhov/yii2-json-api": "*" to the require section of your composer.json file.
This implementation is not complete yet. Yii2 JSON Api extension is still under hard development process. Content Negotiation and Document Structure are only covered at the moment.
Once the extension is installed, simply use it in your code by : Data Serializing and Content Negotiation:
Controller:
class Controller extends \yii\rest\Controller { public $serializer = 'tuyakhov\jsonapi\Serializer'; public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'contentNegotiator' => [ 'class' => ContentNegotiator::className(), 'formats' => [ 'application/vnd.api+json' => Response::FORMAT_JSON, ], ] ]); } }Model:
use tuyakhov\jsonapi\ResourceTrait; use tuyakhov\jsonapi\ResourceInterface; class User extends ActiveRecord implements ResourceInterface { use ResourceTrait; }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
'request' => [ 'parsers' => [ 'application/vnd.api+json' => 'tuyakhov\jsonapi\JsonApiParser', ] ]


