我使用Yii2并创建了RESTful API服务。但在我的应用程序中,我想返回所有响应的预定义密钥的JSON数据。例如:
默认响应:
[
{
"id": 1,
"title": "Brooklyn"
},
{
"id": 2,
"title": "Financial District"
},
{
"id": 4,
"title": "Social District"
}
]
但我希望得到那样的东西:
"data": [
{
"id": 1,
"title": "Brooklyn"
},
{
"id": 2,
"title": "Financial District"
},
{
"id": 4,
"title": "Social District"
}
]
答案 0 :(得分:1)
您只需在控制器中自定义rest serializer:
public $serializer = [
'class' => 'yii\rest\Serializer',
'collectionEnvelope' => 'data',
];