I
I
Ivan Komarov2022-03-23 09:43:43
Yii
Ivan Komarov, 2022-03-23 09:43:43

How to do different serialization of data for two different actions in yii2 REST API?

Hello.
Below is the code for my controller with two actions.
I need to use one serializer for one action and another for the second one.
How it is better to implement it?

Thank you for your attention.

namespace app\modules\v1\controllers;

use yii;
use yii\web\BadRequestHttpException;

class PortfolioController extends ActiveController
{
    public $modelClass = Portfolio::class;

    public $serializer = [
        'class' => 'common\components\serializers\PortfolioSerializer',
    ];

    public function actionIndex()
    {    
          return $data;    
    }


    public function actionAdd()
    {    
           return $data;
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2022-03-23
@FreeTibet

public $serializer = PortfolioSerializer::class;

// ...

public function actionAdd()
{
    $this->serializer = AnotherPortfolioSerializer::class;
    // ...
    return $data;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question