CakePHP 3将数组转换为XML

时间:2016-02-04 05:36:27

标签: cakephp cakephp-3.0

我有一个数组需要转换为XML视图并在视图页面中显示相同的数组。该数组看起来像这样:

   Array
(
 [0] => Cake\ORM\Entity Object
    (
        [id] => 1
        [user_id] => 3
        [title] => Article 1
        [body] => Validation is commonly used.
        [created] => Cake\I18n\Time Object
            (
                [time] => 2016-01-29T13:51:43+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [modified] => Cake\I18n\Time Object
            (
                [time] => 2016-01-30T13:51:43+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [[new]] => 
        [[accessible]] => Array
            (
                [*] => 1
            )

        [[dirty]] => Array
            (
            )

        [[original]] => Array
            (
            )

        [[virtual]] => Array
            (
            )

        [[errors]] => Array
            (
            )

        [[repository]] => AppManager.Articles
    )

[1] => Cake\ORM\Entity Object
    (
        [id] => 2
        [user_id] => 1
        [title] => Article 2
        [body] => The API documentation
        [created] => Cake\I18n\Time Object
            (
                [time] => 2016-01-03T13:51:58+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [modified] => Cake\I18n\Time Object
            (
                [time] => 2016-01-03T13:51:58+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [[new]] => 
        [[accessible]] => Array
            (
                [*] => 1
            )

        [[dirty]] => Array
            (
            )

        [[original]] => Array
            (
            )

        [[virtual]] => Array
            (
            )

        [[errors]] => Array
            (
            )

        [[repository]] => AppManager.Articles
    )

[2] => Cake\ORM\Entity Object
    (
        [id] => 3
        [user_id] => 2
        [title] => Article 3
        [body] => Now that you’ve created a validator and added the rules you want to it, you can start using it to validate data. Validators are able to validate array data. For example, if you wanted to validate a contact form before creating and sending an email you could do the following:
        [created] => Cake\I18n\Time Object
            (
                [time] => 2016-01-03T13:52:19+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [modified] => Cake\I18n\Time Object
            (
                [time] => 2016-01-03T13:52:19+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [[new]] => 
        [[accessible]] => Array
            (
                [*] => 1
            )

        [[dirty]] => Array
            (
            )

        [[original]] => Array
            (
            )

        [[virtual]] => Array
            (
            )

        [[errors]] => Array
            (
            )

        [[repository]] => AppManager.Articles
    )

[3] => Cake\ORM\Entity Object
    (
        [id] => 8
        [user_id] => 1
        [title] => Sample webservices post
        [body] => Get the cookies from the response. Cookies will be returned as an array with all the properties that were defined in the response header. To access the raw cookie data you can use header()
        [created] => Cake\I18n\Time Object
            (
                [time] => 2016-02-02T11:06:14+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [modified] => Cake\I18n\Time Object
            (
                [time] => 2016-02-02T11:06:14+0000
                [timezone] => UTC
                [fixedNowTime] => 
            )

        [[new]] => 
        [[accessible]] => Array
            (
                [*] => 1
            )

        [[dirty]] => Array
            (
            )

        [[original]] => Array
            (
            )

        [[virtual]] => Array
            (
            )

        [[errors]] => Array
            (
            )

        [[repository]] => AppManager.Articles
    )

让我告诉你到目前为止我做了什么。

    public function getDataFromWebServices(){

        $articles   =   $this->Articles->find("all")->toArray();

        $this->set([
            "articles"      => $articles,
            "_serialize"    => ["articles"]
        ]);

        // Not sure what to do in the view after serializing this array

        $xmlObject = Xml::fromArray($articles);

        $xmlString = $xmlObject->asXML();  // Gives an error "Invalid Input"

    }

任何帮助都将不胜感激。

和平!的xD

1 个答案:

答案 0 :(得分:1)

  

AppController的

public function initialize()
{
    $this->loadComponent('RequestHandler');
}
  

routes.php文件

$routes->extensions(['xml']);

使用这些设置,您可以使用.xml调用您的观看次数 如果您要序列化变量,那就是。

/controller/get-data-from-web-services.xml

有关CakePHP 3中的json和xml视图的更多信息

http://book.cakephp.org/3.0/en/views/json-and-xml-views.html