Challenge - 5 Problems
Laravel API Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel API route response?
Consider this Laravel API route code. What JSON response will the client receive when accessing
/api/user?Laravel
<?php use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; Route::get('/user', function (Request $request) { return response()->json(['name' => 'Alice', 'role' => 'admin']); });
Attempts:
2 left
💡 Hint
Look at the array passed to
response()->json() and how it converts to JSON.✗ Incorrect
The route returns a JSON response with keys 'name' and 'role' directly, so the output is a JSON object with those keys and values.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a POST API route in Laravel?
You want to create a POST API route at
/api/login that calls LoginController@login. Which option is correct?Attempts:
2 left
💡 Hint
Use the array syntax with the controller class and method for API routes.
✗ Incorrect
Option C uses the correct HTTP verb 'post' and the recommended array syntax with the controller class and method name.
🔧 Debug
advanced2:00remaining
Why does this Laravel API route return a 404 error?
Given this route definition, why does accessing
/api/profile return a 404 error?
Route::get('/user/profile', function() {
return ['profile' => 'data'];
});Attempts:
2 left
💡 Hint
Check the exact URL path defined in the route and the URL you are accessing.
✗ Incorrect
The route is defined for '/user/profile', but the request is to '/api/profile'. They do not match, so Laravel returns 404.
❓ state_output
advanced2:00remaining
What is the output of this Laravel API route with middleware?
This route uses the 'auth:sanctum' middleware. What happens when an unauthenticated user accesses
/api/dashboard?
Route::middleware('auth:sanctum')->get('/dashboard', function () {
return ['message' => 'Welcome to your dashboard'];
});Attempts:
2 left
💡 Hint
Middleware 'auth:sanctum' requires authentication before allowing access.
✗ Incorrect
If the user is not authenticated, the 'auth:sanctum' middleware blocks access and returns a 401 Unauthorized response.
🧠 Conceptual
expert3:00remaining
How does Laravel API route caching affect route changes?
You run
php artisan route:cache in your Laravel project. What is the effect on API routes if you later add a new route in routes/api.php without clearing the cache?Attempts:
2 left
💡 Hint
Route caching stores routes in a compiled file for speed but requires manual refresh after changes.
✗ Incorrect
Route caching compiles all routes into a cache file. New routes added after caching are ignored until cache is cleared and rebuilt.