PHP code example of philiprehberger / laravel-api-versioning

1. Go to this page and download the library: Download philiprehberger/laravel-api-versioning library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

philiprehberger / laravel-api-versioning example snippets


// config/api-versioning.php

return [
    'supported_versions'  => ['v1', 'v2'],
    'default_version'     => 'v1',
    'latest_version'      => 'v2',
    'deprecated_versions' => [],
    'vendor_name'         => 'myapp',
    'header'              => 'X-API-Version',
    'response_headers'    => true,
];

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->alias([
        'api.version' => \PhilipRehberger\ApiVersioning\ApiVersion::class,
    ]);
})

Route::middleware('api.version')->group(function () {
    // ...
});

use PhilipRehberger\ApiVersioning\ApiVersion;

$version = ApiVersion::current($request); // e.g. 'v2'

// config/api-versioning.php

'aliases' => [
    'latest' => 'v2',
    'stable' => 'v1',
],

use PhilipRehberger\ApiVersioning\ApiVersion;

ApiVersion::aliases();                // ['latest' => 'v2', 'stable' => 'v1']
ApiVersion::resolveAlias('latest');   // 'v2'
ApiVersion::resolveAlias('unknown');  // null

use PhilipRehberger\ApiVersioning\ApiVersion;

ApiVersion::isDeprecated('v1'); // true if v1 is in deprecated_versions or not latest_version
ApiVersion::isDeprecated('v2'); // false if v2 === latest_version

'supported_versions' => ['v1', 'v1.2', 'v1.2.3', 'v2'],

// config/api-versioning.php

'accept_header_pattern' => '/application\/json;\s*version=(v\d+(?:\.\d+){0,2})/',
bash
php artisan vendor:publish --tag=api-versioning-config