Laravel Packages

API Version Control in Laravel

Published
API Version Control in Laravel image

API Version Control is a package by Reindert Vetter designed to manage versions of API endpoints elegantly.

This package provides a configuration file where you define the releases and the version capabilities. Later, you can use things like version statements to determine the capabilities of specific versions of your API:

'releases' => [
'GET/orders' => [
'<=1.0' => [
PrepareParameterException::class,
],
],
'(POST|PUT)/orders' => [
'<=2.0' => [
ThrowCustomException::class,
ValidateZipCode::class,
],
'<=1.0' => [
PrepareParameterException::class,
],
],
],

This package has two ways to manage the versions of your API endpoints, using the following techniques:

  1. Version statement
  2. Version middleware

Version statements include classes that mix in the VersionStatement trait. Thus you can determine if the API code is to be executed without checking a particular version of the API everywhere in your code:

// Instead of version checks everywhere...
if (RequestVersion::isAtLeast('2.0')) {
// ...
}
 
// A VersionStatement class instead based on version
if (ValidateZipCode::permitted()) {
// ...
}

Here's an example of the ValidateZipCode class from the README:

namespace App\VersionControl\Orders;
 
use ReindertVetter\ApiVersionControl\Concerns\VersionStatement;
 
class ValidateZipCode
{
use VersionStatement;
}

Version middleware takes it a step further, processing all requests and responses different from the latest version in a middleware:

use Closure;
use Illuminate\Http\Request;
 
class PrepareParameterException
{
/**
* @param $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
// Set the default parameter because it is required in a newer version.
$request->query->set('sort', 'DESC');
 
/** @var \Illuminate\Http\Response $response */
$response = $next($request);
 
return $response;
}
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article