Home  >  Article  >  Backend Development  >  yii2 outputs xml format data

yii2 outputs xml format data

WBOY
WBOYOriginal
2016-07-29 08:51:291009browse

Author: Bailang Source: http://www.manks.top/yii2_xml_response.html.html The copyright of this article belongs to the author, and you are welcome to reprint it. However, this statement must be retained without the author's consent, and the original text must be given in a prominent position on the article page. connection, otherwise we reserve the right to pursue legal liability.

Xml processing in PHP is rarely used in actual development, but it will inevitably be used. When it is used, in summary, it is still a little troublesome.

Let’s take a look at how xml is processed in yii2. It will be easier than you think.

We take outputting data in xml format as an example.

Since it is output, it must involve web requests and responses. If you are not familiar with it, you can first understand the HTTP protocol.

yii2 supports the following return formats, all of which can be customized.

  • HTML: implemented by yiiwebHtmlResponseFormatter.

  • XML: implemented by yiiwebXmlResponseFormatter.

  • JSON: implemented by yiiwebJsonResponseFormatter.

  • JSONP: implemented by yiiwebJsonResponseFormatter.

  • RAW: use this format if you want to send the response directly without applying any formatting.

We are here for XML.

Let’s first look at a simple output of xml format data

publicfunction actionTest () { 
    \Yii::$app->response->format = \yii\web\Response::FORMAT_XML; 
    return [ 
        'message' => 'hello world', 
        'code' => 100,
    ]; 
}

Here we specify the response format FORMAT_XML, and then access this test method to see that xml type data is output on the page

<response><message>hello worldmessage><code>100code>response>

Above The mentioned method is a bit troublesome. It is not so convenient when configuring multiple items. Let’s try to create the response object ourselves

 

publicfunction 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', //根节点                 'itemTag' => 'url', //单元             ],
        ], 
        'data' => [ //要输出的数据             [ 
                'loc' => 'http://********',
            ],
        ],
    ]); 
}

In order to facilitate the following explanation, the above configurations are also done. You can see that we have configured the response format and made some separate configurations, including configuring the root node rootTag, unit itemTag and data type. Some students have noticed that here we have actually implemented a site map output in xml format very simply. Yes, it's that simple.

The above introduces the xml format data output by yii2, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn