Dingo API

repository·master·Indexed 27 days ago

https://github.com/dingo/api

A toolkit for Laravel and Lumen developers to build feature-rich APIs. It provides infrastructure for API versioning, authentication, content negotiation, rate limiting, and response transformation. Includes CLI commands for route caching (api:cache), route listing (api:routes), and documentation generation (api:docs).

Tokens
1.7K
Snippets
1
Records
15
Agent score
93%

What's inside Dingo API

  1. Overview of Dingo API features

    master

    Dingo API provides a set of tools for building APIs in Laravel, including:

    • Content Negotiation: Handling different media types.
    • Multiple Authentication Adapters: Support for various auth methods.
    • API Versioning: Managing different versions of your API.
    • Rate Limiting: Controlling request frequency.
    • Response Transformers and Formatters: Shaping API output.
    • Error and Exception Handling: Standardized error responses.
    • Internal Requests: Making requests to your own API internally.
    • API Blueprint Documentation: Generating documentation from blueprints.
  2. Migrate to the new Dingo API repository

    master
    The dingo/api package is no longer being maintained at this location due to CI integration issues. For active maintenance and updates, switch to the new repository: https://github.com/api-ecosystem-for-laravel/dingo-api.
  3. Use documentation interfaces to override controllers

    master
    When generating documentation, the api:docs command checks if a controller implements an interface that ends with the suffix Docs. If such an interface is found, the command will use that interface instead of the controller class itself to generate the documentation. This allows you to decouple your API documentation logic from your actual controller implementation.
  4. Register the Dingo API LumenServiceProvider

    master
    To use the Dingo API within a Lumen application, you must register the Dingo\Api\Provider\LumenServiceProvider. This provider handles the registration of the API router adapter, configures the api configuration file, and sets up essential middleware and FormRequest handling.
  5. Configure Dingo API via the 'api' config key

    master

    Dingo API uses a configuration file located at config/api.php. To prevent a RuntimeException during boot, you must ensure that either a prefix or a domain is configured if the application is not running in the console.

    Key configuration options used by the service provider include:

    • prefix: The URL prefix for your API.
    • domain: The domain used for your API.
    • version: The default API version.
    • subtype: The API subtype.
    • standardsTree: The standards tree (e.g., vnd).
    • defaultFormat: The default response format.
    • errorFormat: The format used for error responses.
    • debug: Boolean to enable/disable debug mode.
    • auth: Authentication configuration.
    • transformer: Transformer configuration.
    • formats: Response formatters.
    • formatsOptions: Options for response formats.
  6. Use Dingo API route middleware in Lumen

    master

    The LumenServiceProvider registers several built-in route middleware that you can apply to your routes. Use the following keys in your route definitions:

    • api.auth: Handles authentication via Dingo\Api\Http\Middleware\Auth.
    • api.throttle: Handles rate limiting via Dingo\Api\Http\Middleware\RateLimit.
    • api.controllers: Prepares controllers via Dingo\Api\Http\Middleware\PrepareController.
  7. Filter and sort API routes via CLI

    master

    The api:routes command supports several options to filter the route list or change the display format:

    Filtering Options

    • --path=<string>: Filter routes by their URI path.
    • --name=<string>: Filter routes by their name.
    • --versions=<version>: Filter routes by specific version(s). This option accepts multiple values.
    • --scopes=<scope>: Filter routes by specific scope(s). Use the -S flag. This option accepts multiple values.
    • --protected: Show only protected routes.
    • --unprotected: Show only unprotected routes.

    Display and Sorting Options

    • --sort=<column>: Sort the output by a specific column. Valid columns are: domain, method, uri, name, action.
    • --reverse: Reverse the order of the sorted routes.
    • --short: Display an abridged version of the routes containing only Method, URI, Name, and Version(s).
  8. Use Dingo API service aliases

    master

    Dingo API registers several class aliases in the Laravel service container to allow for easy access to its core components. You can resolve these using the following keys:

    AliasResolved Class / Interface
    api.dispatcherDingo\Api\Dispatcher
    api.http.validatorDingo\Api\Http\RequestValidator
    api.http.responseDingo\Api\Http\Response\Factory
    api.routerDingo\Api\Routing\Router
    api.router.adapterDingo\Api\Contract\Routing\Adapter
    api.authDingo\Api\Auth\Auth
    api.limitingDingo\Api\Http\RateLimit\Handler
    api.transformerDingo\Api\Transformer\Factory
    api.urlDingo\Api\Routing\UrlGenerator
    api.exceptionDingo\Api\Exception\Handler and Dingo\Api\Contract\Debug\ExceptionHandler
  9. Reference the `api:docs` CLI flags

    master

    The api:docs command supports the following options to customize the documentation generation process:

    api:docs {--name= : Name of the generated documentation}
    {--use-version= : Version of the documentation to be generated}
    {--output-file= : Output the generated documentation to a file}
    {--include-path= : Path where included documentation files are located}
    {--use-controller= : Specify a controller where to generate documentation for}