How to customize a xml rest return

Hey specialists and experts,

i try to find a solution for customizing a xml response by changing the properties of[size=“1”][size=“2”] the XmlResponseFormatter[/size][/size] in my REST-controller. More precisely, I don’t want the $rootTag = ‘response’, I want an induvidual description of my outer element according to the model class (…same for $itemTag if a collection is requested).

Where and in which way could I solve this small requirement?

Here is a piece of controller code for example:




<?php

namespace app\controllers;


use yii\rest\ActiveController;


class SimplelistController extends ActiveController {

	public function behaviors(){

 			return [ [

   				'class' => 'yii\filters\ContentNegotiator',

   				'formats' => [

   	  				'application/xml' => Response::FORMAT_XML,

   	  				//MAYBE HERE: 'rootTag' => 'rooti',

            	], ], ];

	}

	

	public function actionOutputXml(){

              	$xmlValues = ['test1' => 'value1', 'test2' => 'value2'];

     		//MAYBE HERE: Yii::$app->response->format->rootTag = 'roots';

     		return $xmlValues;

	}

}



Thx for help in advance

greetz badi aka uli

In answering my own question I have the hope to help others with the same issue or maybe there are other workarounds which follows.

I solved the above described problem with an init()-methode in my controller, affected to all methods (…it works also in each method with a single effect).




<?php

namespace app\controllers;


use Yii;

use yii\rest\ActiveController;

use app\models\TestmodelSearch;

use app\models\Testmodel;

use yii\web\Response;


class TestmodelController extends ActiveController {

	public $modelClass   = 'app\models\Testmodel';

	public $defaultAction = 'output-xml';

	

	public function init(){

    	Yii::$app->response->formatters = [

        	Response::FORMAT_XML => [   

            	'class'	=> 'yii\web\XmlResponseFormatter', 

            	'rootTag'   => Testmodel::TABLE_PLURAL, //Constant created in the model class

            	'itemTag'   => Testmodel::TABLE_SINGLE,

        	]

    	];

	}

	

	public function actionOutputXml() {    	

        	//works: Yii::$app->response->formatters = [Response::FORMAT_XML => ['class'=>'yii\web\XmlResponseFormatter', 

   			//                                               		'rootTag'=>Testmodel::TABLE_PLURAL]];

        	$searchModel = new TestmodelSearch();

        	$dataProvider = $searchModel->search(Yii::$app->request->queryParams);


   		return $dataProvider;

	}

}



Thx for visiting my problem, greetz Badi aka uli