Dingo Api Transformer工作不正常。返回数组而不是变换器

时间:2016-07-20 05:48:15

标签: api laravel laravel-5.2 dingo-api

这是我的第一个Dingo Api实施。这是我的

RoleTrasnformer课程

namespace App\Api\V1\Transformers;

use League\Fractal\TransformerAbstract;
use App\Role;

class RoleTransformer extends TransformerAbstract
{

    public function transform(Role $role)
    {

        return [
            'role_id' => (int)$role->id,
            'name'    => $role->name
        ];
    }

}

这是RoleController

namespace App\Http\Controllers;

use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
use Dingo\Api\Http\Response;
use App\Role;
use App\Http\Requests;
use App\Api\V1\Transformers\RoleTransformer;

class RoleController extends Controller
{
    use Helpers;

    public function index(){
        $roles = Role::all();

        return $this->response->collection($roles, new RoleTransformer())
                              ->setStatusCode(200);
    }
}

虽然我应该只获得role_idname

[

    {
        "id": 1,
        "name": "admin",
        "display_name": "Admin",
        "description": "Administrator",
        "created_at": "2016-07-18 08:11:44",
        "updated_at": "2016-07-18 08:11:44"
    },
    {
        "id": 2,
        "name": "director",
        "display_name": "Director",
        "description": "Country Manager",
        "created_at": "2016-07-18 08:11:44",
        "updated_at": "2016-07-18 08:11:44"
    },
    {
        "id": 3,
        "name": "manager",
        "display_name": "Manager",
        "description": "Team Manager",
        "created_at": "2016-07-18 08:11:44",
        "updated_at": "2016-07-18 08:11:44"
    }

]

不确定我做错了什么。请帮我识别故障。

使用Laravel 5.2和Dingo 1.0.x@dev

2 个答案:

答案 0 :(得分:0)

这部分应该是:

from-clause error

或者你可以让一个构造函数只输出id和名字?让我知道它是否有帮助!

答案 1 :(得分:0)

同样的问题,但我有前缀“api”,仍然无法正常工作。用代码调试的时候,响应已经按照我的请求处理了,下面的代码之后,就恢复了!

 //vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php
public function handle($request)
{
.
.
.
    $this->app['events']->dispatch(
            new Events\RequestHandled($request, $response)
        );

        return $response;
}

laravel 5.8.38,野狗 2.4.7