2

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.