jdlxnl/api-version

Add version as prefix to routes and make it available through a facade in Laravel

1.0.0 2021-12-23 10:23 UTC

This package is auto-updated.

Last update: 2024-04-23 16:06:10 UTC


README

Enables you to create an API version prefix, and make it available in your code.

After installation you can:

use Jdlx\ApiVersion\Facade\Version;

// get current version number as int
Version::number();

// Do something only for request to the old api
if(Version::before(2)){

}

// Do something only for request to the new api
if(Version::from(2)){

}

Installation

composer require jdlxnl/api-version

Register the Facade

 // app/Http/kernel.php
 // Add api midleware

   protected $middlewareGroups = [
        'api' => [
            SetApiVersion::class,
          ],
    ];

// app/Config/app.php

  'providers' => [
        ...
        App\Library\ApiVersion\Provider\ApiVersionServiceProvider::class
    ]

 // change base path for swagger
 // app/Http/Documentation/Server


// Add the option to the router
// routes/api.php
$apiRoutes = function () {
    // define routes
};
Route::group(['prefix' => '{version?}', 'where' => ['version' => 'v[0-9]+']], $apiRoutes);