douqiao8370 2011-11-21 12:01
浏览 68
已采纳

CakePHP RestFul WebService正确的文档和可下载的代码

I've been following this tutorial http://book.cakephp.org/2.0/en/development/rest.html for restful web services integration with cakephp over and over and it seems that it´s not working for my purposes because there`s some issues with the xml automatic constructions (wrong documentation maybe?)

Anyway my question is more a petition. Do you know by any case a good downloadable example related to create Rest web services on cakePHP 2.0 framework? you know, where you can see the code, the structure and so on. A simple but a good example.

  • 写回答

1条回答 默认 最新

  • dongsi7759 2011-11-23 11:12
    关注

    Ok I see what happens. there´s an issue between cakePHP query result array construction and the form that the xml helper expect to see it for a proper creation of the xml file. For example you construct an array for get all your posts ($posts = $this->Post->find('all');)

    This is what you get:

       $posts = array(
        'post' => array(
            array(
                'id' => '1',
                'tittle' => 'The title'
            ),
            array(
                'id' => '2',
                'tittle' => 'A title once again'
           )
        )
    );
    

    this what an instruction like $xml = Xml::build($xmlPosts); on cakePHP expect on, for example, in a index.ctp file under your xml folder, inside View/Posts:

    $posts = array(
      'posts' => array(
        'post' => array(
            array(
                'id' => '1',
                'tittle' => 'The title'
            ),
            array(
                'id' => '2',
                'tittle' => 'A title once again'
            )
        )
      )
    );
    $xml = Xml::build($post);
    

    The problem yields in that with XML parser needs a root element in the array.

    the documentation on cakePHP dosen´t tell you that. So be careful on follow the example about REST services if you are a beginner on the cookbook.

    You can fix the issue doing so array rearrangement in the index.ctp xml constructor. Something like this:

    <?php
    $xmlPosts = array('posts' => array('post' => $posts));
    $xml = Xml::build($xmlPosts);
    echo $xml->saveXML();
    ?>
    

    I know is not the optimal solution. But I'm open to more thoughtful answers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题