简体   繁体   English

如何使用ApiGility和RPC创建数据并返回格式正确的json

[英]How to create data and return properly formatted json using ApiGility and RPC

I am using the RPC service of ApiGilty to return some data. 我正在使用ApiGilty的RPC服务返回一些数据。 I would like to double check whether or not this is the correct way of formatting and returning the data as I am not 100% sure of the correct process. 我想仔细检查一下这是否是格式化和返回数据的正确方法,因为我不能百分百确定正确的过程。

EDIT: To clarify The data is being built from a number of entities: 编辑:为了澄清数据是从许多实体中构建的:

main
main_extra
main_data
main_data_days
main_data_tiers

Is there a way to hit main and get all the sub entities? 有没有办法击中main并获得所有sub实体? Currently I am building my data from scratch and returning an array. 目前,我正在从头开始构建数据并返回一个数组。

My RPC Controller is as follows: 我的RPC控制器如下:

use My\Data\Controller\DataInterface;
use Zend\Mvc\Controller\AbstractActionController;
use ZF\ContentNegotiation\ViewModel;

class MyDataController extends AbstractActionController
{
    const GENERAL_ERROR = 'api.rpc.my-data.my-data-controller';

    public function __construct(
        MyDataInterface $myData
    )
    {
        $this->myData = $myData;
    }


    public function myDataAction()
    {

        $my_id    = (int) $this->params()->fromRoute('my_id', 0);

        if ($my_id == 0)
        {
            $data = $this->myData->getMyData();
        } else
        {
            $data = $this->myData->getMyData($my_id);
        }

        $result = new ViewModel(array(
            'data' => $data
        ));

        return $result;

    }

}

Now to create the data I am doing something like this: 现在创建数据,我正在做这样的事情:

public function getMyData( $my_id = null )
{
    $returnArray = [];

    $array1 = [
        'key_1' => [1,2,3,4],
        'key_2' => '123',
        'key_3' => ['a','b','c']
    ];

    $array2 = [
        'key_1' => [1,2,3,4,5,6,7,8],
        'key_2' => '123456',
        'key_3' => ['a','b','c','d']
    ];

    if ($my_id == 1) {
        $array3 = ['some','or','other'];
    } else {$array3 = []; }

    $final_array = [
        'data1' => $array1,
        'data2' => $array2,
        'data3' => $array3
    ];

    $returnArray['data'] = $final_array;
    $returnArray['success'] = 'true';
    $returnArray['reason']  = '';


    return $returnArray;

}

When checking with postman, I get the following: 与邮递员核对时,我得到以下信息:

在此处输入图片说明

Now since I have nothing to reference this against, my question is simply. 现在,由于我对此没有任何参考,我的问题很简单。 Have I gone about this in the correct way and is this how the return code should be formatted? 我是否已经按照正确的方式进行操作,这是应如何格式化返回码?

Thanks! 谢谢!

Right now the Hal plugin is not used to render your result? 现在, Hal插件不用于呈现结果吗? You are responding a custom json object. 您正在响应自定义json对象。 Is this really what you want? 这真的是您想要的吗?

The response you currently return is not formatted according to HAL specifications. 您当前返回的响应未按照HAL规范进行格式化。 A proper HAL response should hold at least a _links key with a self href . 适当的HAL响应应至少包含一个带有self href_links键。 It would be wrong to return this result with Content-Type headers set to application/hal+json . Content-Type标头设置为application/hal+json返回此结果是错误的。 You should use application/json instead. 您应该改用application/json

Here you can find documentation on how to respond HAL from an RPC-contoller . 在这里,您可以找到有关如何从RPC-contoller响应HAL文档

I am not sure what you want to achieve but maybe you can be a bit more specific in your question so others can help out... 我不确定您要实现什么目标,但也许您可以在问题上更具体一些,以便其他人可以帮助您...

Doesn't look too bad, perhaps adhere to a standard such as jsend http://labs.omniti.com/labs/jsend or you could use hal-json, matthew weier o'phinney has a good blog post on this https://mwop.net/blog/2014-03-26-apigility-rpc-with-hal.html 看起来还不错,也许遵循诸如jsend http://labs.omniti.com/labs/jsend之类的标准,或者您可以使用hal-json,那么matthew weier o'phinney在此https上有一篇不错的博客文章: //mwop.net/blog/2014-03-26-apigility-rpc-with-hal.html

Also you don't need to return a view model as you can just return an array and apigility will return JSON. 另外,您不需要返回视图模型,因为您只需返回一个数组即可,而apigility将返回JSON。 You could also write a jsendViewModel if you go down that route. 如果沿着那条路线走,您还可以编写一个jsendViewModel。

Not exactly an answer but hope this helps you! 不完全是一个答案,但是希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM