好的,我看到會發生什麼。 cakephp查詢結果數組的構造和xml幫助程序希望看到它用於正確創建xml文件的形式之間存在一個問題。例如,您構建一個數組,讓所有的帖子($posts = $this->Post->find('all');
)
這是你會得到什麼:
$posts = array(
'post' => array(
array(
'id' => '1',
'tittle' => 'The title'
),
array(
'id' => '2',
'tittle' => 'A title once again'
)
)
);
這是什麼介紹CakePHP像$xml = Xml::build($xmlPosts);
的指令期望的,例如,在index.ctp你的XML文件夾下的文件,請查看/帖子裏面:
$posts = array(
'posts' => array(
'post' => array(
array(
'id' => '1',
'tittle' => 'The title'
),
array(
'id' => '2',
'tittle' => 'A title once again'
)
)
)
);
$xml = Xml::build($post);
問題的產量在與XML解析器需要陣列中的根元素。
關於cakePHP的文檔不會告訴你。因此,如果您是食譜的初學者,請注意遵循有關REST服務的示例。
你可以修復在index.ctp xml構造函數中進行數組重排的問題。這樣的事情:
<?php
$xmlPosts = array('posts' => array('post' => $posts));
$xml = Xml::build($xmlPosts);
echo $xml->saveXML();
?>
我知道不是最佳的解決方案。但我願意接受更深思熟慮的答案。