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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀