问题描述
我使用的是漂亮的Restler REST框架,在数据库中的各种客户端暴露出一些资源。
I'm using the nice Restler REST framework to expose some resources in a database to various clients.
现在返回从服务结果的时候,格式是有点像这样(我使用Restler网站的例子这里的基础):
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": "[email protected]"
}
]
或在XML:
<?xml version="1.0"?>
<response>
<item>
<id>1</id>
<name>Jac Wright</name>
<email>[email protected]</email>
<item>
</response>
什么错误我一点的是,在JSON结构是匿名的(如果这是在这里使用正确的说法?),并且在XML对象被包装在一个项目的标签。我想看到的结构中返回的资源类型名称包装。像这样的:
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": "[email protected]"
}
]
和
<?xml version="1.0"?>
<response>
<author>
<id>1</id>
<name>Jac Wright</name>
<email>[email protected]</email>
<author>
</response>
在情况下,我还没有完全grokked相关code,这是没有改装在所有可能的Restler本身?
In case I haven't completely grokked the relevant code, is this at all possible without modding Restler itself?
推荐答案
借助返回以下
return array(
array('id'=>1, 'name'=>'Jac Wright', 'email'=>'[email protected]'),
array('id'=>2, 'name'=>'Arul Kumaran', 'email'=>'[email protected]' ),
);
我们需要做的是包裹单独的阵列中的关键作者下的另一个数组。例如,在 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'=>'[email protected]'),
array('id'=>2, 'name'=>'Arul Kumaran3','email'=>'[email protected]')
));
}
}
这得到你想要的确切结果都 JSON 和 XML
This gets the exact result that you wanted for both json and xml
author.xml:
author.xml:
<?xml version="1.0"?>
<response>
<author>
<id>1</id>
<name>Jac Wright1</name>
<email>[email protected]</email>
</author>
<author>
<id>2</id>
<name>Arul Kumaran3</name>
<email>[email protected]</email>
</author>
</response>
author.json:
author.json:
{
"author": [
{
"id": 1,
"name": "Jac Wright1",
"email": "[email protected]"
},
{
"id": 2,
"name": "Arul Kumaran3",
"email": "[email protected]"
}
]
}
让我解释一下我使用,以及在技术,我用上述职位的方法,并张贴的确切 XML
结构我想和使用的print_r
来找到相应的 PHP
结构:)
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>[email protected]</email></author><author><id>1</id><name>Jac Wright</name><email>[email protected]</email></author></response>'
和响应
Array
(
[author] => Array
(
[0] => Array
(
[id] => 1
[name] => Jac Wright
[email] => [email protected]
)
[1] => Array
(
[id] => 1
[name] => Jac Wright
[email] => [email protected]
)
)
)
<?xml version="1.0"?>
<response>
<author>
<id>1</id>
<name>Jac Wright</name>
<email>[email protected]</email>
</author>
<author>
<id>1</id>
<name>Jac Wright</name>
<email>[email protected]</email>
</author>
</response>
有关完整性,让我把网关的index.php
以及此处
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:&QUOT;命名&QUOT;返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!