PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
197: API Versioning Strategies in PHP
I've seen this happen a dozen times: a developer pushes a "small" change to a JSON response to make it cleaner, and suddenly, the mobile app team is screaming because their app is crashing in production. It usually looks something like this.
// UserProfileController.php (The "Updated" Version)
public function show($id) {
$user = User::find($id);
return response()->json([
'id' => $user->id,
'first_name' => $user->firstName,
'last_name' => $user->lastName,
'email' => $user->email,
]);
}
The "Breaking Change" Disaster
At first glance, this looks great. It's more granular than the old version. But here is the problem: your existing API clients (like a React Native app or a third-party integration) are expecting a key called full_name. They aren't looking for first_name or last_name. When they try to access data.full_name, they get null or an undefined error, and the UI collapses.
You can't just tell your users to "update their app" immediately. Some users don't update for months. You've just violated the "API Contract"βthe implicit agreement that once you publish a response format, you don't change it without warning.
Isolating Versions via URI Routing
The most straightforward way to fix this is by introducing versioning directly into your URL. This allows the old code to keep running exactly as it was, while new clients can opt into the improved data structure. I prefer this method because it's explicit; you can see exactly which version is being hit in your server logs without digging into request headers.
Here is how I would restructure the logic to support both the legacy and the new format:
// routes/api.php
// Legacy Version 1
Route::prefix('v1')->group(function () {
Route::get('/user/{id}', [Api\V1\UserProfileController::class, 'show']);
});
// Current Version 2
Route::prefix('v2')->group(function () {
Route::get('/user/{id}', [Api\V2\UserProfileController::class, 'show']);
});
By splitting these into different namespaces (Api\V1 and Api\V2), you avoid the nightmare of having a single controller filled with if ($version == 1) blocks. That approach becomes unmaintainable the moment you hit version 3 or 4.
Maintaining the Legacy Contract
Now, you simply move the original logic into the V1 controller. It stays frozen in time. You don't touch it unless there is a critical security bug.
// Api/V1/UserProfileController.php
public function show($id) {
$user = User::find($id);
return response()->json([
'id' => $user->id,
'full_name' => $user->firstName . ' ' . $user->lastName, // The expected contract
'email' => $user->email,
]);
}
// Api/V2/UserProfileController.php
public function show($id) {
$user = User::find($id);
return response()->json([
'id' => $user->id,
'first_name' => $user->firstName,
'last_name' => $user->lastName,
'email' => $user->email,
]);
}
URI vs. Header Versioning
You'll often hear people argue that you should use "Accept Headers" (e.g., Accept: application/vnd.myapi.v2+json) instead of the URL. In a perfect academic world, that's more "RESTful" because the URL represents the resource, not the version of the representation.
In the real world? Header versioning is a pain to test in a browser and often gets stripped by aggressive caching layers or proxies. If you're building a public-facing API, stick to the URI versioning. It's easier for your developers to understand and significantly easier to debug.
- V1: Becomes your "Maintenance Mode" code.
- V2: Becomes your "Active Development" code.
- Deprecation: Once your logs show that 0% of traffic is hitting
/v1/, you can finally delete that folder with a clear conscience.
π Practical Task
Implementing a Versioned Product Catalog API
You are managing a product API. Currently, the /v1/products/{id} endpoint returns a flat structure where the category is just a string. You need to upgrade this to /v2/ where the category is a full object containing an id and a slug.
Your Task:
- Create two separate controller methods:
ProductControllerV1::show()` andProductControllerV2::show()`. - Ensure
V1returns this exact structure:{"id": 1, "name": "Mechanical Keyboard", "category": "Peripherals"}. - Ensure
V2returns this exact structure:{"id": 1, "name": "Mechanical Keyboard", "category": {"id": 10, "name": "Peripherals", "slug": "peripherals"}}. - Set up the routing logic to differentiate between
/api/v1/products/1and/api/v2/products/1.
Test both endpoints to ensure that changing the logic in V2 does not accidentally alter the output of V1.
There are no comments for now.