12

是否有一种简单的方法可以从 Dingo API 响应中删除“数据”信封。

当我使用这个 Transformer 来转换用户模型时:

class UserTransformer extends EloquentModelTransformer
{

    /**
     * List of resources possible to include
     *
     * @var array
     */
    protected $availableIncludes = [
        'roles'
    ];

    protected $defaultIncludes = [
        'roles'
    ];

    public function transform($model)
    {
        if(! $model instanceof User)
            throw new InvalidArgumentException($model);

        return [
            'id' => $model->id,
            'name' => $model->name,
            'email' => $model->email
        ];
    }

    /**
     * Include Roles
     *
     * @param User $user
     * @return \League\Fractal\Resource\Item
     */
    public function includeRoles(User $user)
    {
        $roles = $user->roles;

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

我得到这个回应:

{
data : [
      "id": 102,
      "name": "Simo",
      "email": "mail@outlook.com",
      "roles": {
        "data": [
          {
            "id": 1    
            "name": "admin"
          }
        ]
      }
    }
]
}

我读了一些关于 RESTful API 的文章,其中很多都说这种封装的响应不是很现代(你应该改用 HTTP Header)。

如何至少为包含禁用此行为?

谢谢

4

3 回答 3

14

对于那些后来遇到这个问题并且我真的很难做到的人,我想分享一下我是如何让它在我的 API 中工作的:

1) 创建一个自定义序列化程序 NoDataArraySerializer.php

namespace App\Api\V1\Serializers;

use League\Fractal\Serializer\ArraySerializer;

class NoDataArraySerializer extends ArraySerializer
{
    /**
     * Serialize a collection.
     */
    public function collection($resourceKey, array $data)
    {
        return ($resourceKey) ? [ $resourceKey => $data ] : $data;
    }

    /**
     * Serialize an item.
     */
    public function item($resourceKey, array $data)
    {
        return ($resourceKey) ? [ $resourceKey => $data ] : $data;
    }
}

2)设置新的序列化器。在bootstrap/app.php 中,添加:

$app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
    $fractal = new League\Fractal\Manager;
    $fractal->setSerializer(new App\Api\V1\Serializers\NoDataArraySerializer);
    return new Dingo\Api\Transformer\Adapter\Fractal($fractal);
});

而已。

现在,在您的UserController(例如)中,您可以像这样使用它:

namespace App\Api\V1\Controllers;

use App\Api\V1\Models\User;
use App\Api\V1\Transformers\UserTransformer;

class UserController extends Controller
{
    public function index()
    {
        $items = User::all();

        return $this->response->collection($items, new UserTransformer());
    }
}

响应将如下所示:

[
    {
        "user_id": 1,
        ...
    },
    {
        "user_id": 2,
        ...
    }
]

或者,我想添加一个信封,你只需要在Controller中设置资源键。代替 :

return $this->response->collection($items, new UserTransformer());

经过

return $this->response->collection($items, new UserTransformer(), ['key' => 'users']);

响应将如下所示:

{
    "users": [
        {
            "user_id": 1,
            ...
        },
        {
            "user_id": 2,
            ...
        }
    ]
}
于 2016-02-24T13:08:39.563 回答
4

YouHieng 解决方案的一个补充。NoDataArraySerializer在 Laravel 5.3 及更高版本中注册的首选方法是编写自定义ServiceProvider并将逻辑添加到boot()方法而不是bootstrap/app.php文件中。

例如:

php artisan make:provider DingoSerializerProvider

然后:

public function boot(){
    $this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
        $fractal = new League\Fractal\Manager;
        $fractal->setSerializer(new App\Http\Serializers\NoDataArraySerializer());
        return new Dingo\Api\Transformer\Adapter\Fractal($fractal);
    });
}
于 2017-07-28T20:20:20.983 回答
3

看看http://fractal.thephpleague.com/serializers/#arrayserializer。他们准确地解释了什么时候该做什么

有时人们想删除项目的“数据”命名空间

于 2016-01-19T07:59:58.473 回答