API Versioning in Laravel 11 Note
Laravel News

API Versioning in Laravel 11

Laravel 11's updated application skeleton eliminates unnecessary files, including most service providers and API routes in the default installation.To add an API, use the "php artisan install:api" command to set up API routes and a Sanctum configuration.For versioning APIs, a common approach is to create separate route files for each version, organized in folders like "routes/api/api_v1.php".In Laravel 10 and earlier, API route files were added to the RouteServiceProvider, but in Laravel 11, route bootstrapping has moved to bootstrap/app.php.Routes can now be versioned by adding prefixes to the appropriate route files and then including them in the "routes/api.php" file, which is already within the "api" route group.For example, "routes/api/api_v1.php" and "routes/api/api_v2.php" can be added and referenced in "routes/api.php" using "Route::prefix('v1')->group(base_path('routes/api/api_v1.php'));".Versioned routes can be isolated using the "--path" flag in the "php artisan route:list" command.Alternatively, versioned routes can be defined directly in the bootstrap/app.php file using the "then" argument, but API middleware must be added manually in this approach.By organizing routes into separate files and using prefixes, API versioning becomes clearer and easier to maintain.