Yii2 envelope single data in JSON response

I went through offical guide and found a way to envelop JSON data like this.


use yii\rest\ActiveController;


class UserController extends ActiveController

{

    public $modelClass = 'app\models\User';

    public $serializer = [

        'class' => 'yii\rest\Serializer',

        'collectionEnvelope' => 'items',

    ];

}

This works perfect when I have a collection and then I have a response like this.


{

     products:....

}

But what I want to do is that i have a envelope for single data. For example if I do products/10 GET request to get.


{

    product:

}

Hope somebody figured it out.

Did you try if overwriting $serializer works?







"if $model->count() is 1" ...




    $this->serializer = [

        'class' => 'yii\rest\Serializer',

        'collectionEnvelope' => 'product',

    ];