0
0
Laravelframework~20 mins

Route middleware in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
}
AThe user receives a 403 Forbidden error page.
BThe user sees 'Welcome Admin' message.
CThe user is redirected to the login page.
DThe middleware throws a runtime exception.
Attempts:
2 left
💡 Hint
Think about what abort(403) does in Laravel middleware.
📝 Syntax
intermediate
2: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?
ARoute::middleware(['checkRole'])->group(function () { Route::get('/admin', fn() => 'Admin'); });
BRoute::middleware('checkRole', function () { Route::get('/admin', fn() => 'Admin'); });
CRoute::middleware(['checkRole', 'auth'])->get('/admin', fn() => 'Admin');
DRoute::middleware('checkRole')->group(function () { Route::get('/admin', fn() => 'Admin'); });
Attempts:
2 left
💡 Hint
Middleware names can be passed as a string or array, but group expects array for multiple.
state_output
advanced
2: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');
});
AResponse headers include only X-Middleware-A: A
BResponse headers include X-Middleware-A: A and X-Middleware-B: B
CResponse headers include only X-Middleware-B: B
DResponse has no custom headers
Attempts:
2 left
💡 Hint
Middleware run in order and can add headers to the response.
🔧 Debug
advanced
2: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);
}
Aabort(403) does not stop execution in Laravel middleware.
BMiddleware must call $next() before aborting.
CThe user() method is not available on the request object.
DThe middleware forgets to return the result of $next($request).
Attempts:
2 left
💡 Hint
Middleware must return the next middleware's response.
🧠 Conceptual
expert
2: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?
AMiddleware run randomly depending on server load.
BMiddleware run in reverse order: throttle, then log, then auth.
CMiddleware run in the order listed: auth, then log, then throttle.
DMiddleware run in parallel, order does not matter.
Attempts:
2 left
💡 Hint
Think about how middleware wrap the request in layers.