2011-11-21 43 views
2

我一直在關注本教程http://book.cakephp.org/2.0/en/development/rest.html爲了寧靜的web服務與cakephp反覆集成,似乎它不適合我的目的,因爲有一些問題與XML自動構建(錯誤的文檔可能?)CakePHP RestFul WebService正確的文檔和可下載的代碼

無論如何,我的問題是更多的請願書。你知道在任何情況下一個好的可下載的例子有關在CakePHP 2.0框架上創建Rest Web服務嗎?你知道,你可以在哪裏看到代碼,結構等等。一個簡單但很好的例子。

回答

4

好的,我看到會發生什麼。 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(); 
?> 

我知道不是最佳的解決方案。但我願意接受更深思熟慮的答案。