Menu

Versioning a Laravel REST API without making a mess
📰

Versioning a Laravel REST API without making a mess

DEV Community: laravel·Arun Tyagi·12 days ago
#G0hiFmzO
#devtolaravel#devto#dev#breaking#version
Long article (414 words) — no AI summary yet.
Reading 0:00
15s threshold

Your first API client ships, then the mobile app ships, then a partner integration ships, and suddenly you can't rename a single field. Versioning is how you keep moving without breaking people.

Here's the setup I use on client Laravel APIs.

1. Version in the URL, not headers

Header-based versioning ( Accept: application/vnd.app.v2+json ) is elegant, but URL versioning ( /api/v1/... ) is easier to debug, cache, log and explain to a partner's developer. I default to the URL.

// bootstrap/app.php (Laravel 11+) -> withRouting ( api : __DIR__ . '/../routes/api.php' , then : function ( ) { Route :: middleware ( 'api' ) -> prefix ( 'api/v2' ) -> group ( base_path ( 'routes/api_v2.php' ) ) ; } , )

Enter fullscreen mode

Exit fullscreen mode

2. Version the contract, not the whole codebase

Don't copy every controller into a V2 folder. Most of the time only the response shape changes.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.