Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dingo API remove "data" envelope

is it there an easy way to remove the "data" envelope from the Dingo API response.

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());
    }

I get this response:

{
data : [
      "id": 102,
      "name": "Simo",
      "email": "[email protected]",
      "roles": {
        "data": [
          {
            "id": 1    
            "name": "admin"
          }
        ]
      }
    }
]
}

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?

Thank you

like image 481
Simon Schneider Avatar asked Oct 31 '15 17:10

Simon Schneider


1 Answers

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) 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) 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);
});

That's it.

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());
    }
}

And the response will look like :

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

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());

by

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

And the response will look like :

{
    "users": [
        {
            "user_id": 1,
            ...
        },
        {
            "user_id": 2,
            ...
        }
    ]
}
like image 79
YouHieng Avatar answered Sep 22 '22 05:09

YouHieng