分形API调用不return内容

Fractal API call does not return content

我用 fractal 包编写了一些 API 方法来输出内容。当我调用特定资源时,所有内容 returns 都是空的。

为了检查是否一切正常,我打印了一些内容变量。例如,如果我在索引函数中使用 $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 这是事件转换器:

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 包装器尝试了其他方法。然后一切都很好。但是我想用我写的respond函数来抽象代码。这似乎是问题所在。当我将数据传入函数时,没有返回任何内容。当我转储变量数据时,返回了一个 JSON 响应。但是 returns 数组中的 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
            ]);
        }
    }

但输出仍然是空的。所以一定是有响应功能的东西。

您返回 Response::json() 两次。

$this->respond returns Response::json,还有 $this->respondWithCollection() returns 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();
}