How to Create API Versioning in Laravel 11

Hello, laravel web developers! In this article, we'll see how to create API versioning in laravel 11. In laravel 11, we'll create API versioning like V1, V2, etc. In Laravel 11, the application skeleton was slimmed down to remove extra files that aren't required on every project.

Part of that change removed all service providers from the application source code except the AppServiceProvider. It is removing API routes in the default installation.

Create API Versioning in Laravel 11

How to Create API Versioning in Laravel 11

 

Install Laravel API

In this step, we'll install laravel 11 API using the following command.

php artisan install:api

The install:api sets up the api.php route file (and configures it), database migration for personal access tokens, and a sanctum configuration file.

 

Versioning Your API in Separate Files

A common way to handle versioned APIs in Laravel is by organizing routes into separate files. This makes managing and understanding each API version easier, keeping things neat and organized. In Laravel 10 and earlier, a typical method is to add separate route files for each API version in the RouteServiceProvider.

$this->routes(function () {
    Route::middleware('api')
        ->prefix('api')
        ->group(base_path('routes/api.php'));
 
    Route::middleware('api')
        ->prefix('api/v1')
        ->group(base_path('routes/api_v1.php'));
 
    Route::middleware('api')
        ->prefix('api/v2')
        ->group(base_path('routes/api_v2.php'));
 
    Route::middleware('web')
        ->group(base_path('routes/web.php'));
});

 

Versioning Your API in Laravel 11

In laravel 11, route bootstrapping now moved from the RouteServiceProvider to bootstrap/app.php, here are a few methods you can use to version your API.

touch routes/api_v1.php
touch routes/api_v2.php
 
php artisan make:controller --api Api/V1/PostsController
php artisan make:controller --api Api/V2/PostsController

Next, open the api.php file and add the following code.

routes/api.php

Route::prefix('v1')->group(base_path('routes/api_v1.php'));
Route::prefix('v2')->group(base_path('routes/api_v2.php'));

The above code is in api.php means that we are already working within the api route prefix and using the api middleware group.

Next, let's add the example routes for each respective API version to visualize the route list for each version.

Here's the api_v1.php file:

<?php
 
use App\Http\Controllers\Api\V1\PostsController;
 
Route::apiResource('posts', PostsController::class);

And the api_v2.php file:

<?php
 
use App\Http\Controllers\Api\V2\PostsController;
 
Route::apiResource('posts', PostsController::class);

With our route files defined, running route:list, we can see versioned routes.

php artisan route:list

Also, you can get specific versioned routes using the --path.

php artisan route:list --path=api/v1
php artisan route:list --path=api/v2

 

Defining Versioned Routes in the App Bootstrap File

Another approach is to define additional API routes directly in the bootstrap/app.php file using the then argument, which takes a Closure.

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
        then: function () {
            Route::middleware('api')
                ->prefix('api/v1')
                ->group(base_path('routes/api/api_v1.php'));
 
            Route::middleware('api')
                ->prefix('api/v2')
                ->group(base_path('routes/api/api_v2.php'));
        }
    )
    // ...
;

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS