Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gigo6000 opened this issue Jun 7, 2017 · 6 comments
Open

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

gigo6000 opened this issue Jun 7, 2017 · 6 comments
Assignees

Comments

@gigo6000
Copy link

gigo6000 commented Jun 7, 2017

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

@gigo6000
Copy link
Author

gigo6000 commented Jun 7, 2017

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.

@mtangoo
Copy link

mtangoo commented Jun 9, 2017

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

@gigo6000
Copy link
Author

gigo6000 commented Jun 9, 2017

@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.

@mtangoo
Copy link

mtangoo commented Jun 10, 2017

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

@tuyakhov tuyakhov self-assigned this Jul 8, 2017
@Valkinaz
Copy link

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;
}

@tuyakhov tuyakhov added this to the v0.1.6 milestone Mar 2, 2018
@tuyakhov tuyakhov removed this from the v0.1.6 milestone Mar 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants