Yii2 method _php instance for outputting XML format data

Source: Internet
Author: User
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.

  • 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.