Yii2 the method of outputting XML format Data _php instance

Source: Internet
Author: User
Tags yii

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.