php - Restful API Design Content Output -


i got api output ready great of so-members answering questions. thank way.

but wonder 1 thing in output. when call url receive api content this:

[     {     "data": [     {     "incidentreference": "r-20150405-887f93",     "latitude": 48.259698,     "longitude": 11.434679,     "archived": false     },     (...)     ]     } ] 

i read book "build apis won't hate" , great resource lot of stuff. don't think, output see right. mean, namespacing have. shouldn't this?

{         "data": [         {         "incidentreference": "r-20150405-887f93",         "latitude": 48.259698,         "longitude": 11.434679,         "archived": false         },         (...)         ]  } 

so shouldn't whole thing json only? in case return additionally within array. functions doing job these:

public function index() {     $incidents = incident::all();      if( ! $incidents) {         return response::json([             'error' => [                 'message' => 'there no incidents in database.',                 'code' => 100             ]         ], 404);     } else {         return $this->respond([             $this->respondwithcollection($incidents, new incidenttransformer)         ]);     } }      public function respond($data, $headers = []) {         return response::json($data, $this->getstatuscode(), $headers);     }      protected function respondwithcollection($collection, $callback) {         $resource = new collection($collection, $callback);         $rootscope = $this->fractal->createdata($resource);         return $rootscope->toarray();     } 

so yes, respondwithcollection returns array, handled within respond function states return response::json expect json output when calling resource.

is ok?

the next structure

 {"data" : [{}, {}]}  

is when have fields, such total count of items, number of page, etc:

 {"data" : [{}, {}], "page":1, "total": 100}  

otherwise, use simple structure:

[{"incidentreference": "r-20150405-887f93", ...}, {...}] 

i'd recommend avoid deep-nested structures.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -