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:




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

best way to return custom-formatted model from REST API is use fields() method:

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