Luracast Restler:“命名"返回的对象 [英] Luracast Restler: "Naming" returned objects

查看:31
本文介绍了Luracast Restler:“命名"返回的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用漂亮的 Restler REST 框架向各种客户端公开数据库中的一些资源.

现在当从服务返回结果时,格式有点像这样(我在这里使用 Restler 网站自己的示例作为基础):

<预><代码>[{身份证":1,"name": "Jac Wright",电子邮件":jacwright@gmail.com"}]

或在 XML 中:

<响应><项目><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email><项目></响应>

让我有点小毛病的是,在 JSON 中,结构是匿名的(如果这是在这里使用的正确术语?),而在 XML 中,对象被包装在项目"标签中.我希望看到由资源类型名称包装的返回结构.像这样:

<预><代码>[作者": {身份证":1,"name": "Jac Wright",电子邮件":jacwright@gmail.com"}]

<响应><作者><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email><作者></响应>

如果我还没有完全理解相关代码,如果不修改 Restler 本身,这完全可能吗?

解决方案

example您已经指出返回以下内容

返回数组(数组('id'=>1, 'name'=>'Jac Wright', 'email'=>'jacwright@gmail.com'),数组('id'=>2, 'name'=>'Arul Kumaran', 'email'=>'arul@luracast.com'),);

我们需要做的就是将单个数组包装在另一个数组中,键为author".例如在 author.php

 数组(数组('id'=>1, 'name'=>'Jac Wright1', 'email'=>'jacwright@gmail.com'),array('id'=>2, 'name'=>'Arul Kumaran3','email'=>'arul@luracast.com')));}}

这将获得您想要的 jsonxml

的确切结果

author.xml:

<响应><作者><id>1</id><name>Jac Wright1</name><email>jacwright@gmail.com</email><作者><id>2</id><name>Arul Kumaran3</name><email>arul@luracast.com</email></响应>

author.json:

<代码>{作者": [{身份证":1,"name": "Jac Wright1",电子邮件":jacwright@gmail.com"},{身份证":2,"name": "Arul Kumaran3",电子邮件":arul@luracast.com"}]}

让我解释一下我使用的技术,我使用了上面的 post 方法并发布了我想要的确切 xml 结构并使用 print_r 找到相应的 <代码>php 结构 :)

这是我在命令行中尝试的 cURL

curl -X POST http://restler2.dev/test/naming_returned/author.xml -H "Content-Type: text/xml" -d ';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>'

和响应

数组([作者] =>大批([0] =>大批([id] =>1[名称] =>杰克·赖特[电子邮件] =>jacwright@gmail.com)[1] =>大批([id] =>1[名称] =>杰克·赖特[电子邮件] =>jacwright@gmail.com)))<?xml version="1.0"?><响应><作者><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email><作者><id>1</id><name>Jac Wright</name><email>jacwright@gmail.com</email></响应>

为了完整起见,让我把网关 index.php 也放在这里

addAPIClass('作者');$r->setSupportedFormats('JsonFormat','XmlFormat');$r->handle();

I'm using the nice Restler REST framework to expose some resources in a database to various clients.

Now when returning results from service, the format is somewhat like this (I'm using the Restler web sites own examples as a basis here):

[
 {
   "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 bugs me a little is that in JSON the structure is anonymous (if that is the correct term to use here?) and that in the XML the object is wrapped in an "item" tag. I'd like to see the structure returned wrapped by the resource type name. 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 haven't completely grokked the relevant code, is this at all possible without modding Restler itself?

解决方案

The example you have pointed out returns the following

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

all we need to do is wrap the individual arrays in another array under the key 'author'. For example in 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')
               ));
    }
}

This gets the exact result that you wanted for both json and 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"
    }
  ]
}

Let me explain the technique that I used as well, I used the above post method and posted the exact xml structure I want and used print_r to find the corresponding php structure :)

Here is the cURL I tried in the command line

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>'

and the 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>

For completeness let me put the gateway index.php as well here

<?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();

这篇关于Luracast Restler:“命名"返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆