RESTful - different fields in different actions

As I undersant I can define additional fields that will be returned by REST-controller with overriding nethod fields in model.


public function fields() {

        $fields = parent::fields();


        $fields['posts'] = function (User $model)

        {

            return $model->posts;

        };


        return $fields;

    }

It’s not good when in different actions you need to show different fields, you should use in model code like this:


if (Yii::$app->controller->action->id == 'index') {...}

Is there a better way to implement this feature?

Any ideas?