php - Fractal API call does not return content -
i worte api methods fractal package output content. when call specific resources, returns empty.
to check if working performed prints of content variables inbetween. example, if take $incidents variable in index function returned entries in database expected.
the same true, when call $collection variable in respondwithcollection method in api controller. data available here well. browser output this:
{
"data": { "headers": {} } }
to keep simple, method show results of database:
class apiincidentscontroller extends apicontroller { protected $incidenttransformer; protected $fractal; function __construct(incidenttransformer $incidenttransformer){ $this->incidenttransformer = $incidenttransformer; $this->beforefilter('auth.basic', ['on' => 'post']); $this->fractal = new manager(); parent::__construct($this->fractal); } 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([ 'data' => $this->respondwithcollection($incidents, new incidenttransformer), ]); } }
the api controller managing these calls this:
class apicontroller extends controller { protected $statuscode = 200; protected $fractal; public function __construct(manager $fractal) { $this->fractal = $fractal; } public function getstatuscode() { return $this->statuscode; } public function setstatuscode($statuscode) { $this->statuscode = $statuscode; return $this; } public function respond($data, $headers = []) { return response::json($data, $this->getstatuscode(), $headers); } protected function respondwithitem($item, $callback) { $resource = new item($item, $callback); $rootscope = $this->fractal->createdata($resource); return $this->respondwitharray($rootscope->toarray()); } protected function respondwitharray(array $array, array $headers = []) { return response::json($array, $this->statuscode, $headers); } protected function respondwithcollection($collection, $callback) { $resource = new collection($collection, $callback); $rootscope = $this->fractal->createdata($resource); return $this->respondwitharray($rootscope->toarray()); }
update 1 incidenttransformer:
use league\fractal\transformerabstract; class incidenttransformer extends transformerabstract { public function transform(incident $incident) { return [ 'incidentreference' => $incident['incidentreference'], 'latitude' => $incident['latitude'], 'longitude' => $incident['longitude'], 'archived' => (boolean) $incident['incidentarchived'] ]; } }
update 2 tried else, removing respond
wrapper. fine. want use respond function wrote abstract code. seems issue. when pass in data function, nothing being returned. when dump variable data, there json response returned. respondwithcollection method within returns array. don't see why happening. issue?
i adapted method this: public function index() { $incidents = incident::all(); if( ! $incidents) { return response::json([ 'error' => [ 'message' => 'there no incidents in database.', 'code' => 100 ] ], 404); } else { $data = $this->respondwithcollection($incidents, new incidenttransformer); return $this->respond([ 'data' => $data ]); } }
but still output empty. must response function.
you returning response::json()
twice.
$this->respond
returns response::json
, $this->respondwithcollection()
returns respondwitharray() does.
try like:
public function index() { $incidents = incident::all(); if( ! $incidents) { return response::json([ 'error' => [ 'message' => 'there no incidents in database.', 'code' => 100 ] ], 404); } else { // getcollection instead of respondwithcollection $data = $this->getcollection($incidents, new incidenttransformer); return $this->respond([ 'data' => $data ]); } }
-
// getcollection instead of respondwithcollection protected function getcollection($collection, $callback) { $resource = new collection($collection, $callback); $rootscope = $this->fractal->createdata($resource); // don't respond again //return $this->respondwitharray($rootscope->toarray()); return $rootscope->toarray(); }
Comments
Post a Comment