PHP in the processing of XML, although the actual development of the current use of less, but will inevitably use, when it is used, summed up or a little bit of trouble.
Let's take a look at how XML is handled in YII2. It's easier than you think.
Let's take the output XML format data as an example.
Since it is the output, it is bound to involve the Web request and response, unfamiliar to the first to understand the next HTTP protocol.
The following return formats are supported in YII2 and can be customized.
Html:implemented by Yii\web\htmlresponseformatter.
Xml:implemented by Yii\web\xmlresponseformatter.
Json:implemented by Yii\web\jsonresponseformatter.
Jsonp:implemented by Yii\web\jsonresponseformatter.
Raw:use This format if you want to send the response directly without applying any formatting.
We're just going for the XML.
Let's look at a simple output XML format data
Public Function Actiontest () {\yii:: $app->response->format = \yii\web\response::format_xml; return [' Message ' =& Gt ' Hello World ', ' code ' = 100,]; }
Here we specify the reponse response format format_xml, and then access the test method to see the output of XML type data on the page
Hello World
100
The above mentioned way is a bit troublesome, trouble in the configuration of multiple items when it is not so convenient, we have to create a Reponse object to try
Public Function Actiontest () {return \yii::createobject ([' class ' = ' yii\web\response ', ' format ' = = \yii\web\resp Onse::format_xml, ' formatters ' and [\yii\web\response::format_xml = ' class ' = ' yii\web\ Xmlresponseformatter ', ' roottag ' = ' urlset ',//root node ' itemtag ' + ' url ',//unit],], ' data ' = = [' loc ' = > ' http://******** ',],],]); }
In order to facilitate the following instructions, the above configuration, you can see that we have configured the format of the response, a separate configuration, including the configuration of the root node Roottag, cell Itemtag and data types. Some students notice, here in fact, we are very simple to implement a site map of the XML format output. Yes, it's that simple.