Yii2.控制器中的操作(方法):
public function actionGet()
{
Yii::$app->response->format = Response::FORMAT_XML;
return [
'items' => [
['id' => 'data'],
['id' => 'body'],
],
];
}
在输出时获取 XML:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<items>
<item>
<id>data</id>
</item>
<item>
<id>body</id>
</item>
</items>
</response>
如何在 XML 标记中添加属性?并删除response
标签:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item update="true">
<id>data</id>
</item>
<item update="false" new="true">
<id>body</id>
</item>
</items>
文档未显示此情况。
你不能。
此功能目前不受支持,也不在路线图中。您必须构建自己的 ResponseFormatter(实现 http://www.yiiframework.com/doc-2.0/yii-web-responseformatterinterface.html)才能实现此目的。
请参阅:https://github.com/yiisoft/yii2/issues/5996
此外,无法删除response
标记。您可以通过设置格式化程序的rootTag
值来重命名根标记。http://www.yiiframework.com/doc-2.0/yii-web-xmlresponseformatter.html#%24rootTag-detail