Laravel:Dingo / API分页自定义根密钥 [英] Laravel : Dingo/API Pagination custom root key
问题描述
根据文档,我使用了分页,我的代码看起来像这样
$ users = User :: paginate(50);
return $ this-> response-> paginator($ users,new UserTransformer);
不幸的是,响应根键是数据
{
data:[
{
id:1,
username:superuser
...
我想更改数据键因为在我的情况下,emberjs得到这个响应,并尝试与不存在的基准模型建立链接,关键是需要设置与ember模型相同的名称,以防万一RESTAdapter。
我已经在响应中尝试了一些参数,但没有改变
return $ this-> response-> paginator($ users,new UserTransformer,['key'=>'users']);
或
return $ this-> response-> paginator($ users,new UserTransformer,['identifier'=>'users']);
没有任何工作,我被卡住了数据键。
有人有解决方案吗?
提前谢谢。
我设法解决了我的问题。
我不修改api.php配置,变压器保持不变
'transformer'=> env('API_TRANSFORMER',Dingo\Api\Transformer\Adapter\Fractal :: class),
首先我创建一个新的序列化器
// app / Api / V1 / Serializers / CustomJsonSerializer.php
<?php命名空间App \Api\V1\Serializers;
使用League \Fractal\Pagination\CursorInterface;
使用League \Fractal\Pagination\PaginatorInterface;
使用League\Fractal\Serializer\SerializerAbstract;
/ **
*在项目中创建一个新的Serializer
* /
使用League \Fractal\Serializer\ArraySerializer;
class CustomJsonSerializer扩展ArraySerializer
{
public function collection($ resourceKey,array $ data)
{
if($ resourceKey === false) {
return $ data;
}
返回数组($ resourceKey?:'data'=> $ data);
}
public function item($ resourceKey,array $ data)
{
if($ resourceKey === false){
return $ data ;
}
返回数组($ resourceKey?:'data'=> $ data);
}
}
我将新的自定义序列化程序设置在AppServiceProviders
// app \Providers\AppServiceProviders.php
<?php
命名空间App \Providers;
使用Illuminate\Support\ServiceProvider;
使用Dingo\Api\Transformer\Adapter\Fractal;
使用League\Fractal\Manager;
使用App \Api\V1\Serializers\CustomJsonSerializer;
class AppServiceProvider扩展ServiceProvider
{
/ **
*引导任何应用程序服务。
*
* @return void
* /
public function boot()
{
$ this-> app ['Dingo\Api\\ setTransformer\Factory'] - > setAdapter(function($ app){
$ fractal = new Manager();
$ fractal-> setSerializer(new CustomJsonSerializer());
返回新的分形($ fractal);
});
}
/ **
*注册任何应用服务。
*
* @return void
* /
public function register()
{
//
}
}
我希望它会帮助ppl:)
I've developed an API with Laravel 5 and Dingo/API.
Following the documentation, i used pagination and my code look like that
$users = User::paginate(50);
return $this->response->paginator($users, new UserTransformer);
Unfortunately, the response root key is "data"
{
"data": [
{
"id": 1,
"username": "superuser",
......
I'd like to change the "data" key to a custom one, because in my case, emberjs get this response and try to make a link with a "datum" model which doesn't exist, the key need to be set with the same name as the ember model in case of a RESTAdapter.
I already tried some parameters in the response but nothing change
return $this->response->paginator($users, new UserTransformer, ['key' => 'users']);
or
return $this->response->paginator($users, new UserTransformer, ['identifier' => 'users']);
Nothing work, i'm stuck with the "data" key.
Is someone have a solution ?
Thank you in advance.
I managed to fix my problem.
I don't modify the api.php configuration, transformer stay the same
'transformer' => env('API_TRANSFORMER', Dingo\Api\Transformer\Adapter\Fractal::class),
Firstly i create a new serializer
// app/Api/V1/Serializers/CustomJsonSerializer.php
<?php namespace App\Api\V1\Serializers;
use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Serializer\SerializerAbstract;
/**
* Create a new Serializer in your project
*/
use League\Fractal\Serializer\ArraySerializer;
class CustomJsonSerializer extends ArraySerializer
{
public function collection($resourceKey, array $data)
{
if ($resourceKey === false) {
return $data;
}
return array($resourceKey ?: 'data' => $data);
}
public function item($resourceKey, array $data)
{
if ($resourceKey === false) {
return $data;
}
return array($resourceKey ?: 'data' => $data);
}
}
And i set my new custom serializer inside the AppServiceProviders
// app\Providers\AppServiceProviders.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Dingo\Api\Transformer\Adapter\Fractal;
use League\Fractal\Manager;
use App\Api\V1\Serializers\CustomJsonSerializer;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
$fractal = new Manager();
$fractal->setSerializer(new CustomJsonSerializer());
return new Fractal($fractal);
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
I hope it'll help ppl :)
这篇关于Laravel:Dingo / API分页自定义根密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!