我用分形包对一些API方法进行了输出以输出内容。当我调用特定资源时,一切都会返回空白。
要检查一切是否正常,我在中间执行了一些内容变量的打印。例如,如果我在索引函数中获取$ incidents变量,则会按预期返回数据库中的所有条目。
当我在API控制器的respondWithCollection方法中调用$ collection变量时,情况也是如此。这里也有数据。但浏览器输出只是这样:
{
"data": {
"headers": {}
}
}
为了简单起见,这是显示数据库所有结果的方法:
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 are no incidents in the database.',
'code' => 100
]
], 404);
} else {
return $this->respond([
'data' => $this->respondWithCollection($incidents, new IncidentTransformer),
]);
}
}
管理这些调用的API控制器是:
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());
}
更新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']
];
}
}
更新2
我通过删除respond
包装器尝试了其他方法。一切都很好。但我想使用我编写的响应函数来抽象代码。这似乎是个问题。当我将数据传入函数时,没有返回任何内容。当我转储变量数据时,返回了一个JSON响应。但是respondWithCollection方法返回一个数组。我不明白为什么会这样。这可能是问题吗?
I adapted the method like this:
public function index()
{
$incidents = Incident::all();
if( ! $incidents) {
return Response::json([
'error' => [
'message' => 'There are no incidents in the database.',
'code' => 100
]
], 404);
} else {
$data = $this->respondWithCollection($incidents, new IncidentTransformer);
return $this->respond([
'data' => $data
]);
}
}
但输出仍为空。所以它必须是具有响应功能的东西。
答案 0 :(得分:1)
您将返回Response::json()
两次。
$this->respond
会返回Response::json
,但$this->respondWithCollection()
也会返回respondWithArray()。
尝试类似:
public function index()
{
$incidents = Incident::all();
if( ! $incidents) {
return Response::json([
'error' => [
'message' => 'There are no incidents in the 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();
}