Laravel & Lumen RESTFul API Expansion pack: Dingo API (v)--converter (Transformer)

Source: Internet
Author: User

1. Introduction

With the converter, you can convert an object to an array and force conversions of Integer and Boolean types, including pagination results and nested associations.

In this chapter we mainly discuss converters and their use, where the converter consists of the following two layers of meaning:

    • The conversion layer (transformation layer) is a library for preparing and processing converters
    • A converter (transformer) is a class that acquires raw data and translates it into an array format, depending on the conversion layer.

2. Using Converters

There are several ways to use converters.

registering a converter for a class

When you register a converter for a class, you can return the class from the route (assuming it can be converted to an array) and automatically pass the converter processing:

App (' Dingo\api\transformer\factory ')->register (' User ', ' Usertransformer ');

This is useful in the simple API of using the model, because you can return the model directly from the route.

using the Response builder

Refer to the Response Builder section.

2, Fractal

Fractal is the default conversion layer of the Dingo API, which provides a range of useful features to maintain data consistency.

Before using fractal, it is recommended to read the official documentation carefully.

fully automatic correlation relationship craving loading

When embedding affinity relationships using the built-in features of fractal, ensure that they are named consistent with the naming in the model. The expansion pack will automatically load these associations for your craving.

Advanced Configuration

Fractal is registered as the default conversion layer with the default configuration, to manually configure the Dingo\api\transformer\adapter\fractal instance that needs to be instantiated 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 usage through the Response builder

Using fractal in conjunction with the response Builder is typically 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 ']);

Using callbacks

The Fractal conversion layer allows you to register a callback that is triggered after the resource is created, and this callback receives League\fractal\resource\item or League\fractal\resource\collection as the first parameter, The League\fractal\manager instance as the second parameter. You can then use this callback to interact with the resource at a more complex level.

The most common use case is to set up a 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 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 make more customizations to your data conversion, you can implement your own conversion layer in the Dingo API. This requires creating a class that implements Dingo\api\contract\transformer\adapter and implementing 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 way you can add the other methods you want. The purpose of the Transform method is to obtain the $response, which is then passed to the conversion layer with the $transformer, and the conversion layer returns an array, which is eventually returned by the transform method. If your conversion layer is simple, you can implement all of the logic entirely in this class.

$bindings parameters are useful when you have more features in your conversion layer, such as adding metadata or allowing developers to interact with the conversion layer through callbacks.

The $request parameter is the HTTP request that is currently being executed, which is useful when your conversion layer needs to query string parameters or other related data.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.