PHP in the processing of XML, although the actual development of the current use of less, but will inevitably use, when used, summed up or slightly a little bit of trouble.
Let's take a look at how the XML is processed in the YII2. It's going to be more simple than you think.
Let's take an example of outputting data in XML format.
Since it is the output, must involve the Web request and response, unfamiliar can first go to understand the HTTP protocol.
The following return formats are supported in YII2 to customize the configuration.
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 your want to send the response directly without applying any formatting.
We're just coming at 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 ' => ' Hello World ',
' code ' =>,
];
}
Here we specify the reponse response format format_xml, and then access this test method to see the data on the page that is output from the XML type
<response>
<message>hello world</message>
<code>100</code>
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 their own reponse objects to try
Public Function Actiontest () {return
\yii::createobject ([
' class ' => ' Yii\web\response ',
' format ' = > \yii\web\response::format_xml,
' formatters ' => [
\yii\web\response::format_xml => [
' class ' => ' Yii\web\xmlresponseformatter ',
' Roottag ' => ' urlset ',//root node
' itemtag ' => ' url ',//unit ',
],
' data ' => [///To Output
[
' 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 noticed that here in fact we are very simple to achieve a site map of the XML format output. Yes, it's that simple.