Luracast Restler: Returned Naming Objects

I use the beautiful REST Restorer infrastructure to map some resources in the database to various clients.

Now, when you return the results from the service, the format looks something like this (I use my own examples of Restler websites as a basis):

[
 {
   "id": 1,
   "name": "Jac Wright",
   "email": "jacwright@gmail.com"
 }
]

Or in XML:

<?xml version="1.0"?>
<response>
  <item>
    <id>1</id>
    <name>Jac Wright</name>
    <email>jacwright@gmail.com</email>
  <item>
</response>

What offends me a bit is that in JSON the structure is anonymous (if this is the correct term to use here?), And in XML the object is wrapped with the tag "item". I would like the structure to return wrapped with the name of the resource type. Like this:

[
 "author": {
   "id": 1,
   "name": "Jac Wright",
   "email": "jacwright@gmail.com"
 }
]

and

<?xml version="1.0"?>
<response>
  <author>
    <id>1</id>
    <name>Jac Wright</name>
    <email>jacwright@gmail.com</email>
  <author>
</response>

In case I did not fully check the corresponding code, is this even possible without modifying the Restler itself?

+2
1

,

return array(
    array('id'=>1,  'name'=>'Jac Wright',   'email'=>'jacwright@gmail.com'),
    array('id'=>2,  'name'=>'Arul Kumaran', 'email'=>'arul@luracast.com'  ),
);

, , "". , author.php

<?php
class Author {
    function post ($request_data){
        print_r($request_data);
        return $request_data;
    }
    function get() {
        return array('author'=> array(
                    array('id'=>1,  'name'=>'Jac Wright1', 'email'=>'jacwright@gmail.com'),
                    array('id'=>2,  'name'=>'Arul Kumaran3','email'=>'arul@luracast.com')
               ));
    }
}

, json xml

author.xml:

<?xml version="1.0"?>
<response>
  <author>
    <id>1</id>
    <name>Jac Wright1</name>
    <email>jacwright@gmail.com</email>
  </author>
  <author>
    <id>2</id>
    <name>Arul Kumaran3</name>
    <email>arul@luracast.com</email>
  </author>
</response>

author.json:

{
  "author": [
    {
      "id": 1,
      "name": "Jac Wright1",
      "email": "jacwright@gmail.com"
    },
    {
      "id": 2,
      "name": "Arul Kumaran3",
      "email": "arul@luracast.com"
    }
  ]
}

, , xml, , print_r, php:)

cURL,

curl -X POST http://restler2.dev/test/naming_returned/author.xml -H "Content-Type: text/xml" -d '<response><author><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email></author><author><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email></author></response>'

Array
(
    [author] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [name] => Jac Wright
                    [email] => jacwright@gmail.com
                )

            [1] => Array
                (
                    [id] => 1
                    [name] => Jac Wright
                    [email] => jacwright@gmail.com
                )

        )

)
<?xml version="1.0"?>
<response>
  <author>
    <id>1</id>
    <name>Jac Wright</name>
    <email>jacwright@gmail.com</email>
  </author>
  <author>
    <id>1</id>
    <name>Jac Wright</name>
    <email>jacwright@gmail.com</email>
  </author>
</response>

index.php

<?php
require_once '../../restler/restler.php';

#set autoloader
#do not use spl_autoload_register with out parameter
#it will disable the autoloading of formats
spl_autoload_register('spl_autoload');

$r = new Restler();
$r->addAPIClass('Author');
$r->setSupportedFormats('JsonFormat','XmlFormat');
$r->handle();
+3

Source: https://habr.com/ru/post/1619227/


All Articles