Laravel & LumenRESTFulAPI extension package: DingoAPI (1) -- install and configure

Source: Internet
Author: User
Tags subdomain subdomain name
Laravel & LumenRESTFulAPI extension package: DingoAPI (1) -- install and configure Dingo API provides developers with a complete set of tools to help you easily and quickly build your own APIs. These tools include:

  • Content Negotiation
  • Multi-Authentication Adapter
  • API version
  • Frequency limit
  • Response conversion and formatting
  • Error and exception handling
  • Internal request
  • API documentation
1. Installation

Before installing the extension package, make sure that you have installed the following programs:

  • Laravel 5.1 + or Lumen 5.1 +
  • PHP 5.5.9 +

Then run the following Composer command to install the extension package:

composer require dingo/api:1.0.x@dev

After the installation is complete, the operation depends on the framework you are using.

Laravel

Register the service provider in config/app. php to the providers array:

Dingo\Api\Provider\LaravelServiceProvider::class

If you want to customize the extension package configuration, you can publish it to the config Directory:

php artisan vendor:publish --provider="Dingo\Api\Provider\LaravelServiceProvider"
Lumen

Open bootstrap/app. php to register the required service provider:

$app->register(Dingo\Api\Provider\LumenServiceProvider::class);
Facade

There are two facades to process this extension package. you can add any one:

  • Dingo \ Api \ Facade \ API
  • Dingo \ Api \ Facade \ Route
2. configuration

Basically, all functions are pre-configured, so you can start to build an API without any changes. you can use it. the env file implements most of the configurations. you need to release the configuration file (Laravel) for fine-tuning or in the bootstrap/app. in php, you can also use the boot method of AppServiceProvider.

Standard tree

There are three different trees: x, prs, and vnd. The standard tree you use depends on your project:

  • Unregistered tree (x) is mainly used in local or private environments
  • Personal tree (prs) is mainly used for non-commercial projects.
  • The supplier tree (vnd) is mainly used for public and commercial projects.

You can configure in. env:

API_STANDARDS_TREE=vnd
Subtype)

The child type is usually the abbreviation of an application or project, or the lower case format. You can also configure in. env:

API_SUBTYPE=myapp
Prefix and subdomain name

If you have developed an API, you should know that most APIs provide services in the form of subdomain names (or a prefix). the prefix or subdomain name is required and there is only one, do not use the version number as the prefix or subdomain name, because the version number is usually processed through the Accept header.

You can configure the prefix in the. env file:

API_PREFIX=api

Or subdomain name:

API_DOMAIN=api.myapp.com
Version

The version here refers to the default version of the API, which can be used when no version is provided or used as the default version when the API document is generated.

You can configure it in the. env file:

API_VERSION=v1
Name

The API name is used only when the API Blueprint command is used to generate a document. this name is the default name, so you do not need to manually specify the name when generating the document.

You can configure it in the. env file:

API_NAME=My API
Conditional Requests

Because the client cache function is used when caching API requests, conditional requests are enabled by default. you can configure this option in the configuration file. env:

API_CONDITIONAL_REQUEST=false
Strict Mode

Strict mode requires the client to send the Accept header instead of the version specified in the configuration file by default, which means you cannot browse the API through a Web browser.

If the Strict mode is enabled and an invalid Accept header is used, the API throws a Symfony \ Component \ HttpKernel \ Exception \ BadRequestHttpException Exception.

You can configure this option in. env:

API_STRICT=false
Certification Provider

By default, only basic authentication is enabled. if you want to perform more complex authentication configurations, you need to set it in the service provider (Laravel) or startup file (Lumen:

$app['Dingo\Api\Auth\Auth']->extend('oauth', function ($app) {   return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);});
Throttling/frequency limit

The default frequency limit is disabled. you can register a custom throttle with the frequency limit or use an existing authentication and cancel the authentication throttle.

To perform more complex configurations, you also need to perform operations in the service provider or startup file:

$app['Dingo\Api\Http\RateLimit\Handler']->extend(function ($app) {    return new Dingo\Api\Http\RateLimit\Throttle\Authenticated;});
Response converter

Fractal is the default response converter. You can configure it in. env, but to implement more complex configurations, you still need to perform operations in the service provider or startup file:

$app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {    $fractal = new League\Fractal\Manager;    $fractal->setSerializer(new League\Fractal\Serializer\JsonApiSerializer);    return new Dingo\Api\Transformer\Adapter\Fractal($fractal);});
Response format

The default response format is JSON. you can configure the default response format in the configuration file. env:

API_DEFAULT_FORMAT=json

For more advanced format configuration, you need to publish the configuration file to the config directory or operate in the service provider and startup file:

Dingo\Api\Http\Response::addFormatter('json', new Dingo\Api\Http\Response\Format\Jsonp);
Incorrect format

If the extension package encounters an error, it will try to generate an error response instead of throwing the exception to the user. the error format can be customized. This configuration must be implemented in the corresponding configuration file (Laravel) or startup file (Lumen) in the config Directory:

$app['Dingo\Api\Exception\Handler']->setErrorFormat([    'error' => [        'message' => ':message',        'errors' => ':errors',        'code' => ':code',        'status_code' => ':status_code',        'debug' => ':debug'    ]]);
Debugging Mode

If debug mode is enabled, the generated error information will be added to the debug key of the extended package and displayed together with the stack trace information.

You can configure in the configuration file. env:

API_DEBUG=true

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.