Challenge - 5 Problems
Laravel Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when accessing a route with middleware that aborts on condition?
Consider a Laravel route with middleware that checks if a user is admin. If not, it aborts with 403. What happens when a non-admin user accesses this route?
Laravel
<?php Route::middleware(['checkAdmin'])->get('/dashboard', function () { return 'Welcome Admin'; }); // Middleware checkAdmin public function handle($request, Closure $next) { if (! $request->user()?->isAdmin()) { abort(403, 'Unauthorized'); } return $next($request); }
Attempts:
2 left
💡 Hint
Think about what abort(403) does in Laravel middleware.
✗ Incorrect
The middleware checks if the user is admin. If not, it aborts with a 403 error, so the user sees a 403 Forbidden page.
📝 Syntax
intermediate2:00remaining
Which middleware registration syntax is correct in Laravel 10?
You want to register a route middleware named 'checkRole' in Laravel 10. Which syntax is correct in the RouteServiceProvider or routes file?
Attempts:
2 left
💡 Hint
Middleware names can be passed as a string or array, but group expects array for multiple.
✗ Incorrect
Laravel expects middleware names as an array when grouping routes. Option A correctly uses an array with one middleware.
❓ state_output
advanced2:00remaining
What is the output of a route with multiple middleware modifying request data?
Given two middleware that add headers to the response, what headers will the client receive?
Laravel
<?php
// Middleware A
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->header('X-Middleware-A', 'A');
}
// Middleware B
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->header('X-Middleware-B', 'B');
}
Route::middleware(['middlewareA', 'middlewareB'])->get('/test', function () {
return response('OK');
});Attempts:
2 left
💡 Hint
Middleware run in order and can add headers to the response.
✗ Incorrect
Both middleware add their own headers after calling the next middleware, so both headers appear in the final response.
🔧 Debug
advanced2:00remaining
Why does this middleware not stop unauthorized users?
This middleware is supposed to abort unauthorized users but does not. Why?
Laravel
<?php
public function handle($request, Closure $next)
{
if (! $request->user()?->isAdmin()) {
abort(403);
}
return $next($request);
}Attempts:
2 left
💡 Hint
Middleware must return the next middleware's response.
✗ Incorrect
Middleware must return the result of $next($request). Without return, the response is null and the request continues incorrectly.
🧠 Conceptual
expert2:00remaining
How does Laravel determine the order of middleware execution in a route?
Given a route with middleware ['auth', 'log', 'throttle'], in what order are the middleware executed and why?
Attempts:
2 left
💡 Hint
Think about how middleware wrap the request in layers.
✗ Incorrect
Laravel executes middleware in the order they are listed, wrapping the request through each middleware sequentially.