Laravel & LumenRESTFulAPI extension package: DingoAPI (5) -- Transformer)
1. Introduction
With the converter, you can convert objects into arrays and forcibly convert integer and Boolean types, including paging results and nested associations.
In this section, we mainly discuss the converter and its usage. the converter here includes the following two meanings:
- Transformation layer is a library for preparing and processing converters.
- Transformer is a class that obtains raw data and converts it to an array format. the processing method of the processor depends on the conversion layer.
2. use converter
There are multiple ways to use the converter.
Register a converter for a class
After registering a converter for a class, you can return the class from the route (assuming it can be converted to an array) and automatically process it through the converter:
app('Dingo\Api\Transformer\Factory')->register('User', 'UserTransformer');
This is useful in simple APIs that use models, because you can directly return models from routes.
Use the response builder
Refer to the response builder section.
2. Fractal
Fractal is the default conversion layer of Dingo APIs. It provides a series of useful features to maintain data consistency.
Before using Fractal, read its official documentation.
Fully automated link-based eager loading
When Fractal's built-in functions are used to embed associations, ensure that their names are consistent with those in the model. The extension package automatically loads these associations as you desire.
Advanced Configuration
Fractal registers as the default conversion layer with the default configuration. to manually configure it, you need to instantiate the Dingo \ Api \ Transformer \ Adapter \ Fractal instance in the service provider:
$this->app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) { return new Dingo\Api\Transformer\Adapter\Fractal(new League\Fractal\Manager, 'include', ',');});
If you are using Lumen, you can do this in the startup file (bootstrap. php:
app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) { return new Dingo\Api\Transformer\Adapter\Fractal(new League\Fractal\Manager, 'include', ',');});
Advanced use through response builder
Using Fractal in combination with the response builder is usually the best way to return data from the controller. The item, collection, and paginator methods on the response builder can receive additional parameters for custom Fractal.
Resource key
return $this->item($user, new UserTransformer, ['key' => 'user']);
Use callback
The Fractal conversion layer allows you to register a callback triggered after a Resource is created. This callback receives League \ Fractal \ Resource \ Item or League \ Fractal \ Resource \ Collection as the first parameter, league \ Fractal \ Manager instance as the second parameter. Then you can use this callback to interact with resources at a more complex level.
The most common use case is to set the paging cursor or modify the response serial:
return $this->collection($users, new UserTransformer, [], function ($resource, $fractal) { $resource->setCursor($cursor);});
If you do not want to pass the resource key parameter, you can omit this empty array:
return $this->collection($users, new UserTransformer, function ($resource, $fractal) { $fractal->setSerializer(new CustomSerializer);});
3. Custom conversion layer
If you want to customize data conversion, you can implement your own conversion layer in Dingo API. Create a class that implements Dingo \ Api \ Contract \ Transformer \ Adapter and implement the transform method:
use Dingo\Api\Http\Request;use Dingo\Api\Transformer\Binding;use Dingo\Api\Contract\Transformer\Adapter;class MyCustomTransformer implements Adapter{ public function transform($response, $transformer, Binding $binding, Request $request) { // Make a call to your transformation layer to transformer the given response. }}
Transform is the only required method. you can add other methods as needed. The purpose of the transform method is to get $ response and pass it to the conversion layer for processing together with $ transformer. then, the conversion layer returns an array, which is finally returned by the transform method. If your conversion layer is simple, you can implement all the logic in this class.
The $ bindings parameter is useful when your conversion layer contains more features such as adding metadata or allowing developers to interact with the conversion layer through callback.
$ Request is the currently executed HTTP request. it is useful when your conversion layer needs to query string parameters or other related data.