A Laravel package to manage versions of endpoints in an elegant way

Overview

API version control

A Laravel package to manage versions of endpoints in an elegant way

Two ways to manage the versions of your endpoints

Option 1: Version Statement

You probably use if statements to determine whether the code should be executed from a particular version (for example if (RequestVersion::isAtLeast('2.0')) {). But what do you do if you want to run this code for 2 endpoints, one from version 2.0 and the other from version 3.0? This package offers a clean solution for this: Version statement.

Option 2: Version Middleware

Legacy code can get in the way quickly. Do you therefore create multiple controllers to separate the old code from the new code? How do you do this if there are 10 versions at a given time? By then, will you also have 10 validation schemes and response classes for each endpoint? This package also offers a SOLID solution that goes even further than Version statement: Version middleware.

You can use Version middleware and Version statement together in one project

Benefits

Version Statement Version Middleware
Upgrading all endpoints or one specific endpoint. ✔️ ✔️
One overview of all versions with the adjustments. ✔️ ✔️
The controller (and further) always contains the latest version. ✔️
Old versions are only defined once. Once made, you don't have to worry about that anymore. ✔️

Note for Version middleware: If you do not yet use a self-made middleware, you can debug from your controller. With Version middleware, colleagues must now understand that (only with an old version of an endpoint) the code in a middleware also influences the rest of the code.

How to use

Releases

In api_version_control.php config file you will see releases with an array of versions:

    'releases' => [

        'GET/orders' => [
            '<=1.0' => [
                PrepareParameterException::class,
            ],
        ],

        '(POST|PUT)/orders' => [
            '<=2.0' => [
                ThrowCustomException::class,
                ValidateZipCode::class,
            ],
            '<=1.0' => [
                PrepareParameterException::class,
            ],
        ],

        'default' => [
            '<=1.0' => [
                ThrowCustomException::class,
            ],
        ],

    ],

URI match

The URI match contains a string to match the endpoint with regex (GET/orders). The subject contains the method and the URI. It runs through the version rules. If a match is found, it stops searching. The match contains Version rules.

If no URI match can be found, default will be used. That way you can update all your other endpoints.

Version rule

Version rules contains a string with an operator and a version ('<=2.0'). Supported operators are: <, <=, >, >=, ==, !=. All classes within the Version rules with a match are used. The classes within Version rule are Version statement and Version middleware.

Version Statement

A Version Statement file looks like this:


declare(strict_types=1);

namespace App\VersionControl\Orders;

use ReindertVetter\ApiVersionControl\Concerns\VersionStatement;

class ValidateZipCode
{
    use VersionStatement;
}

If the file contains the trait ReindertVetter\ApiVersionControl\Concerns\VersionStatement, then you can do the following in your source code:

if (ValidateZipCode::permitted()) {
    (...)
}

Version Middleware

You process all requests and responses what is different from the latest version in middlewares. You can adjust the request with multiple middlewares to match the latest version. You can also adjust the format of a response in the Version Middleware.

A Version Middleware file (that changing the request) can looks like this:


declare(strict_types=1);

namespace App\Middleware\Version;

use Closure;
use Illuminate\Http\Request;

class PrepareParameterException
{
    /**
     * @param           $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // Set the default parameter because it is required in a newer version.
        $request->query->set('sort', 'DESC');

        /** @var \Illuminate\Http\Response $response */
        $response = $next($request);

        return $response;
    }
}

A Version Middleware file (that changing the response) can looks like this:

[ [ "human" => $response->exception->getMessage(), ], ], ] ); } return $response; } } ">

declare(strict_types=1);

namespace App\Middleware\Version;

use Closure;
use Illuminate\Http\Request;

class ThrowHumanException
{
    /**
     * @param           $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        /** @var \Illuminate\Http\Response $response */
        $response = $next($request);

        // Catch the exception to return an exception in a different format.
        if ($response->exception) {
            $response->setContent(
                [
                    "errors" => [
                        [
                            "human" => $response->exception->getMessage(),
                        ],
                    ],
                ]
            );
        }

        return $response;
    }
}

Request and Resource binding

In a Version Middleware you can use Laravel Binding. So you can bind a FormRequest or a Resource to handle other versions. That way you can more easily support different parameters with rules and you can more easily support different resources. A controller that supports different versions could look like:

    public function index(OrderIndexRequest $request, OrderResource $resource): ResourceCollection
    {
        $orders = Order::query()
            ->productIds($request->productIds())
            ->with($resource->withRelationships())
            ->paginate($request->limit());

        return $resource::collection($orders);
    }

The $request can be either OrderIndexRequestV1 or OrderIndexRequestV2 and the $resource can be either OrderResourceV1 or OrderResourceV2.

You have to create a BindOrderIndexRequestV1 and a BindOrderIndexRequestV2 middleware in which you will bind the request:

class BindOrderIndexRequestV2
{
    public function handle(Request $request, Closure $next)
    {
        app()->bind(OrderIndexRequest::class, OrderIndexRequestV2::class);

        return $next($request);
    }
}

OrderIndexRequestV2 must extend the base class OrderIndexRequest. You can do the same for the resource class. The configuration will then look like this:

        '(GET)/orders' => [
            '<=1' => [
                BindOrderIndexRequestV1::class,
                BindOrderResourceV1::class,
            ],
            '>=2' => [
                BindOrderIndexRequestV2::class,
                BindOrderResourceV2::class,
            ],
        ],

If it's not quite clear yet, post your question in the discussion.

Version parser

Out of the box this package supports versions in the header accept and versions in the URI. But you can also create your own version parser. Specify this in api_version_control.php config file.

Install

  1. Run composer require reindert-vetter/api-version-control.
  2. Add ->middleware(['api', ApiVersionControl::class]) in your RouteServiceProvider.
  3. Create config file by running php artisan vendor:publish --provider='ReindertVetter\ApiVersionControl\ApiVersionControlServiceProvider'.
  4. Choose a Version parser or create one yourself.
Comments
  • Trouble getting version statement to work

    Trouble getting version statement to work

    https://github.com/reindert-vetter/api-version-control/blob/4f7c46a2aeae0189fa92d2aee363a6eb8c586a7d/src/Collection/MiddlewareCollection.php#L81

    Getting exception Attempt to assign property "permit" on string when I am trying to implement a test version statement.

    bug question 
    opened by VFAndrew 8
  • Unable to publish service provider configuration

    Unable to publish service provider configuration

    Hello

    I was installing the package into a Laravel 8.4 installation, and received the below error when trying to publish the config

    Below the trace of the commands + the output

    > composer require reindert-vetter/api-version-control
    Using version ^2.1 for reindert-vetter/api-version-control
    ./composer.json has been updated
    Running composer update reindert-vetter/api-version-control
    Loading composer repositories with package information
    Updating dependencies
    Lock file operations: 1 install, 0 updates, 0 removals
      - Locking reindert-vetter/api-version-control (2.1.5)
    Writing lock file
    Installing dependencies from lock file (including require-dev)
    Package operations: 1 install, 0 updates, 0 removals
      - Downloading reindert-vetter/api-version-control (2.1.5)
      - Installing reindert-vetter/api-version-control (2.1.5): Extracting archive
    Package sebastian/resource-operations is abandoned, you should avoid using it. No replacement was suggested.
    Generating optimized autoload files
    Class App\Http\Controllers\Auth\V1\ResetPasswordController located in ./app/Http/Controllers/API/V1/ResetPasswordController.php does not comply with psr-4 autoloading standard. Skipping.
    > Illuminate\Foundation\ComposerScripts::postAutoloadDump
    > @php artisan package:discover --ansi
    Discovered Package: facade/ignition
    Discovered Package: fideloper/proxy
    Discovered Package: fruitcake/laravel-cors
    Discovered Package: kreait/laravel-firebase
    Discovered Package: laravel-notification-channels/fcm
    Discovered Package: laravel/sail
    Discovered Package: laravel/sanctum
    Discovered Package: laravel/scout
    Discovered Package: laravel/socialite
    Discovered Package: laravel/tinker
    Discovered Package: nesbot/carbon
    Discovered Package: nunomaduro/collision
    Discovered Package: sentry/sentry-laravel
    Discovered Package: spatie/laravel-http-logger
    Discovered Package: spatie/laravel-permission
    Package manifest generated successfully.
    91 packages you are using are looking for funding.
    Use the `composer fund` command to find out more!
    > php artisan vendor:publish --provider='ReindertVetter\ApiVersionControl\ApiVersionControlServiceProvider'
    Unable to locate publishable resources.
    Publishing complete.
    

    Appreciate the help!

    bug documentation 
    opened by jadsalhani 6
  • Another route is already using that name

    Another route is already using that name

    Given documented example for defining api routes without version prefix in addition to versioned routes.

    Route::middleware(['api', ApiVersionControl::class])
        ->prefix('api')
        ->group(base_path('routes/api.php'));
    
    Route::middleware(['api', ApiVersionControl::class])
        ->prefix('api/{version}')
        ->where(['version', '#[a-z]\d{1,3}#'])
        ->group(base_path('routes/api.php'));
    

    I was unable to cache my named routes as it was warning me that the name was already in use. I mitigated this issue by defining a group as name.

    Route::middleware(['api', ApiVersionControl::class])
        ->prefix('api')
        ->as('default.')
        ->group(base_path('routes/api.php'));
    
    Route::middleware(['api', ApiVersionControl::class])
        ->prefix('api/{version}')
        ->where(['version', '#[a-z]\d{1,3}#'])
        ->group(base_path('routes/api.php'));
    
    documentation 
    opened by VFAndrew 5
  • Step 2 of install example is using wrong syntax for route regular expression parameters

    Step 2 of install example is using wrong syntax for route regular expression parameters

    It looks like currently the documentation is using slightly wrong syntax for Regular Expression Parameter Constraints

    Current documentation is using syntax ->where(['version', '#[a-z]\d{1,3}#'])

    but i believe this should either be ->where('version', '#[a-z]\d{1,3}#') or ->where(['version' => '#[a-z]\d{1,3}#'])

    documentation 
    opened by VFAndrew 3
  • Does not work for Laravel 8

    Does not work for Laravel 8

    Laravel Framework 8.55.0

    [InvalidArgumentException]
    Package reindert-vetter/api-version-control has a PHP requirement incompatible with your PHP version, PHP extensions and Composer version

    opened by datacenter-automation 2
Releases(2.2.4)
Owner
Reindert
Reindert
Otpify is a Laravel package that provides a simple and elegant way to generate and validate one time passwords.

Laravel Otpify ?? Introduction Otpify is a Laravel package that provides a simple and elegant way to generate and validate one time passwords. Install

Prasanth Jayakumar 2 Sep 2, 2022
Validate your input data in a simple way, an easy way and right way. no framework required. For simple or large. project.

wepesi_validation this module will help to do your own input validation from http request POST or GET. INTEGRATION The integration is the simple thing

Boss 4 Dec 17, 2022
A mostly useless package to display framework versions at the bottom of the Admin navigation panel.

A mostly useless package to display framework versions at the bottom of the Filament Admin navigation panel and an optional widget to do the same in the dashboard or custom pages.

Adam Weston 10 Nov 8, 2022
This is a courier api endpoints library for interacting such as e-courier, dhl, pathao etc

This is a courier api endpoints library for interacting such as e-courier, dhl, pathao etc Installation Step 1: composer require xenon/multicourier S

Ariful Islam 19 Sep 8, 2022
Simples endpoints para recurso "Pessoa"

Simples endpoints para recurso "Pessoa" Endpoints para "Pessoa" utilizando segregação de interfaces e desacoplamento com o framwork Laravel v8.75. POS

Aroldo Santos 1 Jan 20, 2022
An elegant package for integrate laravel with openwa

Whatsapp Laravel An elegant package to integrate Laravel with Whatsapp automate Features Function Reference Send text Send contact Send media (doc, im

Ardzz Jay Steve 7 Aug 21, 2022
Kalibrant - a package that provides a simple way to manage your models settings

Introduction For your laravel 9.x applications, Kalibrant is a package that provides a simple way to manage your models settings. It is a simple way t

Starfolk 3 Jun 18, 2022
Module for PageBuilder Support for M2.4.3 and future versions.

Magento 2 DataPatchCreator Page Builder Compatibility Plugin Plugin for PageBuilder to remove encoding of HTML special characters done by Magento\Page

eCommerce Nanobots 4 Dec 27, 2022
📝 Artisan Menu - Use Artisan via an elegant console GUI

?? Artisan Menu Use Artisan via an elegant console GUI Features Run built-in and custom Artisan commands from a console GUI Prompts to enter required

Jordan Hall 149 Dec 29, 2022
📝 Artisan Menu - Use Artisan via an elegant console GUI

?? Artisan Menu Use Artisan via an elegant console GUI Features Run built-in and custom Artisan commands from a console GUI Prompts to enter required

Jordan Hall 148 Nov 29, 2022
HydePHP - Elegant and Powerful Static App Builder

HydePHP - Elegant and Powerful Static App Builder Make static websites, blogs, and documentation pages with the tools you already know and love. About

HydePHP 31 Dec 29, 2022
A simple package to manage the creation of a structure composed of the service and repository layers in a Laravel application

Chapolim Este projeto tem como objetivo fornecer alguns comandos adicionais à interface de linha de comando do Laravel, o Artisan, para manipular a es

Eliezer Alves 51 Dec 11, 2022
Laravel package for manage your URL redirects in database or other sources to get better SEO results

Laravel 8 and 9 package to manage URL redirections inside your Laravel application using different data sources. It allows a better SEO support for your Laravel site.

Siro Díaz Palazón 51 Sep 21, 2022
This Laravel Nova package allows you to manage media and media fields

Nova Media Hub This Laravel Nova package allows you to manage media and media fields. Requirements php: >=8.0 laravel/nova: ^4.0 Features Media Hub UI

outl1ne 25 Dec 22, 2022
Save Model is a Laravel package that allows you to save data in the database in a new way.

Save Model is a Laravel package that allows you to save data in the database in a new way. No need to worry about $guarded and $fillable properties in the model anymore. Just relax an use Save Model package.

Laratips 27 Mar 2, 2022
Modularize your laravel app in a package way.

Laravel Modular Pustaka laravel untuk modularisasi kode secara rapi dan mudah di maintain. Instalasi $ composer require kodepandai/laravel-modular Set

Kode Pandai 3 Jun 19, 2022
This package gives Eloquent models the ability to manage their friendships.

Laravel 5 Friendships This package gives Eloquent models the ability to manage their friendships. You can easily design a Facebook like Friend System.

Alex Kyriakidis 690 Nov 27, 2022
An easy way to get vendor and package data from Packagist via API calls

Laravel Packagist Laravel Packagist (LaravelPackagist) is a package for Laravel 5 to interact with the packagist api quickly and easily. Table of cont

Jeremy Kenedy 5 Jul 18, 2022
This package aims to help you standardize all your API responses in a simple and structured way.

Laravel API Response This package aims to help you standardize all your API responses in a simple and structured way. By default, the stucture of the

Kode Pandai 6 Dec 6, 2022