0
0
Laravelframework~20 mins

API versioning patterns in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Versioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does Laravel route versioning affect API response?

Given these Laravel routes for API versioning, what will be the output when calling /api/v2/users?

Route::prefix('api/v1')->group(function () {
    Route::get('users', fn() => 'Users from v1');
});
Route::prefix('api/v2')->group(function () {
    Route::get('users', fn() => 'Users from v2');
});
Laravel
Route::prefix('api/v1')->group(function () {
    Route::get('users', fn() => 'Users from v1');
});
Route::prefix('api/v2')->group(function () {
    Route::get('users', fn() => 'Users from v2');
});
AReturns 'Users from v2'
BReturns 'Users from v1'
CReturns a 404 Not Found error
DReturns both 'Users from v1' and 'Users from v2' concatenated
Attempts:
2 left
💡 Hint

Check which route prefix matches the requested URL.

📝 Syntax
intermediate
2:00remaining
Identify the correct Laravel route versioning syntax

Which option correctly defines a versioned API route for version 3 in Laravel?

ARoute::prefix('api/v3')->group(function () { Route::get('items', fn() => 'v3 items'); });
BRoute::version('v3')->group(function () { Route::get('items', fn() => 'v3 items'); });
CRoute::group('api/v3', function () { Route::get('items', fn() => 'v3 items'); });
DRoute::apiVersion('v3')->group(function () { Route::get('items', fn() => 'v3 items'); });
Attempts:
2 left
💡 Hint

Laravel uses prefix to group routes under a URL segment.

🔧 Debug
advanced
2:00remaining
Why is this Laravel API versioning code not recommended?

Consider this Laravel route setup:

Route::prefix('api')->group(function () {
    Route::get('v1/users', fn() => 'v1 users');
    Route::get('v2/users', fn() => 'v2 users');
});

Why is this not the preferred pattern for API versioning?

Laravel
Route::prefix('api')->group(function () {
    Route::get('v1/users', fn() => 'v1 users');
    Route::get('v2/users', fn() => 'v2 users');
});
ABecause the route definitions are missing a namespace declaration
BBecause the route prefix 'api' conflicts with the version in the path, causing no match
CBecause the HTTP method GET is not allowed for these routes
DBecause the route should use nested prefix groups for versions instead of including version in path
Attempts:
2 left
💡 Hint

Think about how Laravel matches prefixes and paths.

state_output
advanced
2:00remaining
What is the output of this Laravel API versioning middleware example?

Given this middleware that sets API version in request and this route:

class ApiVersionMiddleware {
    public function handle($request, $next) {
        $version = $request->header('Accept-Version', 'v1');
        $request->merge(['api_version' => $version]);
        return $next($request);
    }
}

Route::middleware([ApiVersionMiddleware::class])->group(function () {
    Route::get('api/users', function ($request) {
        return 'Version: ' . $request->api_version;
    });
});

What will be the output when calling /api/users with header Accept-Version: v2?

Laravel
class ApiVersionMiddleware {
    public function handle($request, $next) {
        $version = $request->header('Accept-Version', 'v1');
        $request->merge(['api_version' => $version]);
        return $next($request);
    }
}

Route::middleware([ApiVersionMiddleware::class])->group(function () {
    Route::get('api/users', function ($request) {
        return 'Version: ' . $request->api_version;
    });
});
AVersion: null
BVersion: v1
CVersion: v2
DThrows an error due to missing api_version property
Attempts:
2 left
💡 Hint

Check how the middleware reads and sets the version from headers.

🧠 Conceptual
expert
3:00remaining
Which Laravel API versioning pattern best supports backward compatibility?

Among these API versioning patterns in Laravel, which one best supports backward compatibility while allowing new features?

ANo versioning, always update the same endpoints
BURI versioning using route prefixes like /api/v1/, /api/v2/
CQuery parameter versioning like /api/users?version=1
DHeader versioning using custom headers like Accept-Version
Attempts:
2 left
💡 Hint

Think about how clients can choose which API version to call.