PHP code example of shahghasiadil / laravel-api-versioning
1. Go to this page and download the library: Download shahghasiadil/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/ */
shahghasiadil / laravel-api-versioning example snippets
return [
'default_version' => '2.0',
'supported_versions' => ['1.0', '1.1', '2.0', '2.1'],
'detection_methods' => [
'header' => ['enabled' => true, 'header_name' => 'X-API-Version'],
'query' => ['enabled' => true, 'parameter_name' => 'api-version'],
'path' => ['enabled' => true, 'prefix' => 'api/v'],
'media_type' => ['enabled' => false, 'format' => 'application/vnd.api+json;version=%s'],
],
];
use Illuminate\Support\Facades\Route;
Route::middleware('api.version')->group(function () {
Route::apiResource('users', UserController::class);
});
use ShahGhasiAdil\LaravelApiVersioning\Attributes\ApiVersion;
use ShahGhasiAdil\LaravelApiVersioning\Attributes\Deprecated;
use ShahGhasiAdil\LaravelApiVersioning\Attributes\MapToApiVersion;
use ShahGhasiAdil\LaravelApiVersioning\Traits\HasApiVersionAttributes;
#[ApiVersion(['2.0', '2.1'])]
class UserController extends Controller
{
use HasApiVersionAttributes;
public function index(): JsonResponse
{
return response()->json([
'version' => $this->getCurrentApiVersion(),
'deprecated' => $this->isVersionDeprecated(),
]);
}
#[MapToApiVersion(['2.1'])]
public function store(Request $request): JsonResponse
{
return response()->json(['message' => 'Created']);
}
#[MapToApiVersion(['2.0'])]
#[Deprecated(message: 'Use store() instead', replacedBy: '2.1', sunsetDate: '2026-12-31')]
public function create(Request $request): JsonResponse
{
return response()->json(['message' => 'Deprecated endpoint']);
}
}
#[ApiVersion('2.0')]
#[ApiVersion(['1.0', '1.1', '2.0'])]
#[MapToApiVersion(['2.0', '2.1'])]
use ShahGhasiAdil\LaravelApiVersioning\Attributes\ApiVersionNeutral;
#[ApiVersionNeutral]
class HealthController extends Controller
{
// ...
}
#[Deprecated(
message: 'Use v2 endpoint',
sunsetDate: '2026-12-31',
replacedBy: '2.0'
)]
use Illuminate\Http\Request;
use ShahGhasiAdil\LaravelApiVersioning\Http\Resources\VersionedJsonResource;
class UserResource extends VersionedJsonResource
{
protected function toArrayV1(Request $request): array
{
return ['id' => $this->id, 'name' => $this->name];
}
protected function toArrayV2(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
protected function toArrayDefault(Request $request): array
{
return $this->toArrayV2($request);
}
}
use Illuminate\Http\Request;
use ShahGhasiAdil\LaravelApiVersioning\Http\Resources\VersionedResourceCollection;
class UserCollection extends VersionedResourceCollection
{
protected function toArrayV1(Request $request): array
{
return ['data' => $this->collection];
}
protected function toArrayV2(Request $request): array
{
return [
'data' => $this->collection,
'meta' => ['count' => $this->collection->count()],
];
}
protected function toArrayDefault(Request $request): array
{
return $this->toArrayV2($request);
}
}
use ShahGhasiAdil\LaravelApiVersioning\Services\VersionComparator;
$comparator = app(VersionComparator::class);
$comparator->isGreaterThan('2.0', '1.0');
$comparator->satisfies('2.1', '^2.0');
return [
'default_version' => '1.0',
'detection_methods' => [
'header' => [
'enabled' => true,
'header_name' => 'X-API-Version',
],
'query' => [
'enabled' => true,
'parameter_name' => 'api-version',
],
'path' => [
'enabled' => true,
'prefix' => 'api/v',
],
'media_type' => [
'enabled' => false,
'format' => 'application/vnd.api+json;version=%s',
],
],
'supported_versions' => ['1.0', '1.1', '2.0', '2.1'],
'version_method_mapping' => [
'1.0' => 'toArrayV1',
'1.1' => 'toArrayV11',
'2.0' => 'toArrayV2',
'2.1' => 'toArrayV21',
],
'version_inheritance' => [
'1.1' => '1.0',
'2.1' => '2.0',
],
'default_method' => 'toArrayDefault',
'documentation' => [
'base_url' => env('API_DOCUMENTATION_URL'),
],
'cache' => [
'enabled' => env('API_VERSIONING_CACHE_ENABLED', true),
'ttl' => env('API_VERSIONING_CACHE_TTL', 3600),
],
];
bash
php artisan vendor:publish --provider="ShahGhasiAdil\LaravelApiVersioning\ApiVersioningServiceProvider" --tag="config"
bash
php artisan make:versioned-controller UserController --api-version=2.0
php artisan make:versioned-controller V1UserController --api-version=1.0 --deprecated --sunset=2026-12-31 --replaced-by=2.0
bash
php artisan api:versions
php artisan api:versions --route=users
php artisan api:versions --api-version=2.0
php artisan api:versions --deprecated
php artisan api:versions --compact
php artisan api:versions --json
bash
php artisan api:version:health
bash
php artisan api:version-config --show
php artisan api:version-config --add-version=2.2 --method=toArrayV22
bash
php artisan api:cache:clear
bash
composer analyse