How to get a specific format of a single news item and a news list using the YII 2 REST API?

for one news by id:
{
current:{

},
nex:{

},
previous:{

}
}

for list of news:

[
{…},
{…},

]

Two ways I can think of are:

  1. Change the model requested with your API call: create a new model with “current”, “next” and “previous” as attributes and return it instead of the news model;
  2. Override the actions to return these custom call.

I would go for the first option between those two.

But maybe someone have an even better method.

1 Like

I would suggest you query data with DAO and format the resultant array to your own format and keys

Hm,
best way to return custom-formatted model from REST API is use fields() method: https://www.yiiframework.com/doc/api/2.0/yii-base-model#fields()-detail

Just extend main/common model in api/models and configure fields() method.
Dependency Injection also may be helpful.