0

I am using the RPC service of ApiGilty to return some data. 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? Currently I am building my data from scratch and returning an array.

My RPC Controller is as follows:

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:

enter image description here

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!

Wilt
  • 41,477
  • 12
  • 152
  • 203
HappyCoder
  • 5,985
  • 6
  • 42
  • 73
  • Do you want `json` or `hal+json` response from your controller? I tried to write [an answer](http://stackoverflow.com/a/28691430/1697459) but what you exactly want is not totally clear from your question. – Wilt Feb 24 '15 at 08:52

2 Answers2

1

Right now the Hal plugin is not used to render your result? You are responding a custom json object. Is this really what you want?

The response you currently return is not formatted according to HAL specifications. A proper HAL response should hold at least a _links key with a self href. It would be wrong to return this result with Content-Type headers set to application/hal+json. You should use application/json instead.

Here you can find documentation on how to respond HAL from an RPC-contoller.

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

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Hi - essentially I need to return data from a number of entities. I was not sure if I could simply hit the main entity and get all the sub-data in one nice action in addition how to exclude private data... My solution is to simply build a custom array based on the information I wish to make available... – HappyCoder Feb 24 '15 at 13:29
0

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

Also you don't need to return a view model as you can just return an array and apigility will return JSON. You could also write a jsendViewModel if you go down that route.

Not exactly an answer but hope this helps you!

Purple Hexagon
  • 3,538
  • 2
  • 24
  • 45