0
0
Laravelframework~20 mins

API routes in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel API Routes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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']);
});
A{"name":"Alice"}
B{"user":{"name":"Alice","role":"admin"}}
CSyntaxError: Unexpected token in JSON
D{"name":"Alice","role":"admin"}
Attempts:
2 left
💡 Hint
Look at the array passed to response()->json() and how it converts to JSON.
📝 Syntax
intermediate
2: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?
ARoute::post('/login', 'LoginController@login');
BRoute::get('/login', [LoginController::class, 'login']);
CRoute::post('/login', [LoginController::class, 'login']);
DRoute::post('/login', function() { return LoginController@login(); });
Attempts:
2 left
💡 Hint
Use the array syntax with the controller class and method for API routes.
🔧 Debug
advanced
2: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'];
});
AThe route URL is '/user/profile', so '/api/profile' does not match and returns 404.
BThe route is missing a controller, so it cannot be found.
CThe route method should be post, not get, for '/api/profile'.
DThe route callback returns an array instead of a JSON response, causing 404.
Attempts:
2 left
💡 Hint
Check the exact URL path defined in the route and the URL you are accessing.
state_output
advanced
2: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'];
});
AHTTP 404 Not Found error
BHTTP 401 Unauthorized error response
C{"message":"Welcome to your dashboard"}
DHTTP 500 Internal Server Error
Attempts:
2 left
💡 Hint
Middleware 'auth:sanctum' requires authentication before allowing access.
🧠 Conceptual
expert
3: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?
AThe new route will NOT be recognized until you clear and rebuild the route cache.
BThe new route is automatically detected and works immediately.
CThe application throws a syntax error on any new route added after caching.
DThe new route is added but only accessible via web routes, not API.
Attempts:
2 left
💡 Hint
Route caching stores routes in a compiled file for speed but requires manual refresh after changes.