Dingo API会删除“数据"信封 [英] Dingo API remove "data" envelope
问题描述
是否有一种简便的方法可从Dingo API响应中删除数据"信封.
is it there an easy way to remove the "data" envelope from the Dingo API response.
当我使用此Transformer转换用户模型时:
When I use this Transformer to transform user models:
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标头).
I read some articles about RESTful APIs and a lot of them stated that such enveloped responses arent very modern (You should use the HTTP Header instead).
如何至少对包含对象禁用此行为?
How can I disable this behaviour at least for the includes?
谢谢
推荐答案
对于那些后来遇到这个问题的人,由于我真的很难做到这一点,我想分享一下如何使它在我的API中起作用:
For those who fall on this later and as I had really hard time to make it, I'd like to share how I made it working in my API :
1)创建一个自定义序列化器 NoDataArraySerializer.php :
1) Create a Custom Serializer, 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 中,添加:
2) Set new the Serializer. In bootstrap/app.php, add :
$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 中(例如),您可以像这样使用它:
Now, in your UserController (for instance), you can use it like this :
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中设置资源密钥即可.替换:
Or, I you want to add an enveloppe, you just need to set the resource key in the Controller. Replace :
return $this->response->collection($items, new UserTransformer());
作者
return $this->response->collection($items, new UserTransformer(), ['key' => 'users']);
响应将如下所示:
{
"users": [
{
"user_id": 1,
...
},
{
"user_id": 2,
...
}
]
}
这篇关于Dingo API会删除“数据"信封的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!