0
0
Laravelframework~20 mins

Global 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 happens when a global middleware returns a response early?

In Laravel, if a global middleware returns a response before calling $next($request), what is the effect on the request lifecycle?

Laravel
public function handle($request, Closure $next)
{
    if ($request->header('X-Block')) {
        return response('Blocked by middleware', 403);
    }
    return $next($request);
}
AThe request stops and the response is sent immediately, skipping all other middleware and the controller.
BThe request continues to the next middleware but the response is ignored.
CThe middleware throws an error because it must always call $next($request).
DThe middleware modifies the request but the response is delayed until the controller finishes.
Attempts:
2 left
💡 Hint

Think about what happens if middleware returns a response without calling the next middleware.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this global middleware registration

Which option correctly registers a global middleware in Laravel's app/Http/Kernel.php file?

Laravel
protected $middleware = [
    \App\Http\Middleware\CheckAge::class,
];
A
protected $middleware = [
    App\Http\Middleware\CheckAge::class,
];
B
protected $middleware = [
    \App\Http\Middleware\CheckAge::class,
];
C
protected $middleware = [
    'App\Http\Middleware\CheckAge',
];
D
protected $middleware = [
    \App\Http\Middleware\CheckAge::class
];
Attempts:
2 left
💡 Hint

Remember the correct way to reference a class with namespace in PHP arrays.

state_output
advanced
2:00remaining
What is the output after applying this global middleware?

Given this global middleware that adds a header, what will the client receive?

Laravel
public function handle($request, Closure $next)
{
    $response = $next($request);
    $response->headers->set('X-Global', 'Active');
    return $response;
}
AThe response does not include the <code>X-Global</code> header because headers cannot be modified after $next.
BThe middleware causes a runtime error because headers are immutable.
CThe response includes the header <code>X-Global: Active</code>.
DThe response includes the header <code>X-Global</code> but with an empty value.
Attempts:
2 left
💡 Hint

Think about how middleware can modify the response after calling $next.

🔧 Debug
advanced
2:00remaining
Why does this global middleware not run for some routes?

Consider this global middleware registered in Kernel.php. It does not run for routes using the 'api' middleware group. Why?

Laravel
protected $middleware = [
    \App\Http\Middleware\CheckUser::class,
];

protected $middlewareGroups = [
    'web' => [
        // web middleware
    ],
    'api' => [
        'throttle:api',
        'auth:api',
    ],
];
AThe 'api' middleware group does not include global middleware, but global middleware runs for all routes regardless.
BGlobal middleware always runs for all routes; the issue is elsewhere.
CThe middleware is registered globally but the 'api' routes use a different HTTP kernel instance.
DThe 'api' middleware group overrides global middleware, so global middleware is skipped.
Attempts:
2 left
💡 Hint

Recall how Laravel applies global middleware versus middleware groups.

🧠 Conceptual
expert
2:00remaining
How does Laravel prioritize global middleware compared to route middleware?

In Laravel, when a request is handled, in what order are global middleware and route middleware executed?

AGlobal middleware run only if route middleware allow the request to proceed.
BRoute middleware run first, then global middleware, then controller.
CGlobal and route middleware run simultaneously in parallel before the controller.
DGlobal middleware run first, then route middleware, then controller.
Attempts:
2 left
💡 Hint

Think about the middleware layers and their scope in Laravel.