tuyakhov/yii2-json-api

Is there any demo/example of how to use this extension?

gigo6000 opened this issue · 6 comments

Hi, thanks for your work on this. I'm trying to implement your extension but can't find any example code to play with and I can't figure out how to use it looking at the code. Thanks

I obviously saw the README but that's not really something very helpful, there's no logic in that controller and not a fully working example.

@gigo6000
Explaining what you want exactly will get you quick help. Assuming Yii2 and REST are not your problem here

@mtangoo all I ask is an example of how this extension can be used.

This is how I managed to use the serializer:

modules/api/controllers/UserController.php

<?php                                                                                                                                                                                                    

namespace app\modules\api\controllers;

use yii\rest\Controller;
use yii\helpers\ArrayHelper;
use yii\filters\ContentNegotiator;
use yii\web\Response;
use app\models\User;
use yii\helpers\Json;
use tuyakhov\jsonapi\Serializer;
use yii; 

/**
 * Default controller for the `api` module
 */
class UserController extends Controller
{
    public function behaviors()
    {    
        return ArrayHelper::merge(parent::behaviors(), [
            'contentNegotiator' => [ 
                'class' => ContentNegotiator::className(),
                'formats' => [ 
                    'application/vnd.api+json' => Response::FORMAT_JSON,
                ],   
            ]    
        ]);  
    }    

    /**  
     * 
     * @return string
     */
    public function actionShow()
    {    
        Yii::$app->response->format = Response::FORMAT_JSON;
        $id = Yii::$app->getRequest()->getQueryParam('id');
        $model = User::findOne($id);
        $serializer = new Serializer();
        echo Json::encode($serializer->serialize($model));
    }    
} 

config/web.php

            'rules' => [ 
                [ 'pattern' => 'api/user/<id:\d+>',
                  'route' => 'api/user/show'
                ],  

Hope it helps someone else.

I think you are almost there. According to docs, you need to do one more thing for the controller and two things on the models:

On Controller define public attribute serializer

public $serializer = [
        'class' => 'tuyakhov\jsonapi\Serializer',
        'pluralize' => false,  // makes {"type": "user"}, instead of {"type": "users"}
    ];

Then in models

  1. implement tuyakhov\jsonapi\ResourceInterface
  2. Use the tuyakhov\jsonapi\ResourceTrait trait

This will allow your method to be as clean as

Controller

class UserController extends Controller
{
    public $serializer = [
        'class' => 'tuyakhov\jsonapi\Serializer',
        'pluralize' => false,  // makes {"type": "user"}, instead of {"type": "users"}
    ];
    
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'contentNegotiator' => [
                'class' => ContentNegotiator::className(),
                'formats' => [
                    'application/vnd.api+json' => Response::FORMAT_JSON,
                ],
            ]
        ]);
    }
    
    public function actionShow()
    {    
        $id = Yii::$app->getRequest()->getQueryParam('id');
        return User::findOne($id);
    }  

}

Model

class User extends ActiveRecord implements ResourceInterface
{
    use ResourceTrait;
    
}

HTH

Hi. Thank you for it.
Just want to add example with several models.
It would be great to show this in the documentation.

public function actionSeveral(){
    $models = SomeModel::find()->where([ 'param' => $value ]);
    $response = new ActiveDataProvider([
        'query' => $models,
    ]);
    return $response;
}