Revisions

Resolving the revision

Read the client's revision from a header, the path, the query or your own resolver.

The revision middleware decides the revision of each request. It runs a chain of resolvers: the first one that returns a revision wins.

routes/api.php
Route::middleware('revision')->group(function () {
    Route::get('/orders/{order}', [OrderController::class, 'show']);
});

Configure the chain

config/revisions.php
'resolvers' => [
    HeaderResolver::class => ['name' => 'Api-Revision'],
    // PathResolver::class => ['parameter' => 'revision'],
    // QueryResolver::class => ['name' => 'revision'],
],
'default' => 'latest',
ResolverReadsOption (default)
Laravel\Http\Resolvers\HeaderResolvera request headername (Api-Revision)
Laravel\Http\Resolvers\PathResolvera route parameterparameter (revision)
Laravel\Http\Resolvers\QueryResolvera query parametername (revision)

The options are named constructor arguments; the container injects the rest. When no resolver answers, default applies: latest (the head), oldest, a backed value of the enum, or a resolver class.

What the client gets

Every response carries the revision it was rendered in, under response_header (Api-Revision), and a Vary header on the headers the chain reads. Once the revision's #[Meta] declares them, it also carries:

  • Deprecation: @<unix time> (RFC 9745) from deprecatedAt;
  • Sunset: <HTTP date> from sunsetAt.

An unknown value stops the chain and is answered with 400, listing the accepted values:

GET
/orders/1
Api-Revision: autumn-24
Response400 Bad Request
{
  "message": "Unknown API revision 'autumn-24', expected one of: …",
  "revision": "autumn-24",
  "accepted": ["genesis", "winter-25", "spring-26"]
}

Path mode

With a PathResolver in the chain, Route::revisioned() prefixes its routes with the revision, constrains it to the enum values and adds the middleware:

routes/api.php
Route::revisioned(function (Router $router) {
    // /winter-25/orders/1
    $router->get('/orders/{order}', [OrderController::class, 'show']);
});

The revision parameter never reaches your controllers, and route() generates URLs in the revision of the request.

Pin the revision of an account

A custom resolver implements RevisionResolver. Returning null lets the next resolver of the chain answer.

app/Http/Resolvers/AccountPinnedResolver.php
use Ark4ne\ApiRevisions\Laravel\Http\RevisionResolver;
use Ark4ne\ApiRevisions\Revisions\Revision;

final readonly class AccountPinnedResolver implements RevisionResolver
{
    public function resolve(Request $request): ?Revision
    {
        $account = $request->user();

        return $account instanceof Account ? $account->api_revision : null;
    }
}
config/revisions.php
'resolvers' => [
    AccountPinnedResolver::class => [],
    HeaderResolver::class => ['name' => 'Api-Revision'],
],

Here api_revision is cast to ApiRevision on the model. An account pinned to genesis gets genesis without sending a header; an account with no pinned revision falls through to the header.

A resolver reading request headers also implements Laravel\Http\VariesOn (headers(): array), so that they are listed in the Vary header.