0
0
Laravelframework~20 mins

Registering middleware in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a route uses a middleware registered only in the $middlewareGroups array?

Consider a Laravel app where a middleware named CheckAge is registered only inside the web group in app/Http/Kernel.php. If a route uses middleware('CheckAge') directly, what happens?

Laravel
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\CheckAge::class,
        // other middlewares
    ],
];

// Route definition
Route::get('/profile', function () {
    return 'Profile page';
})->middleware('CheckAge');
AThe middleware is ignored and the route returns 'Profile page'.
BThe middleware runs and the route returns 'Profile page'.
CThe middleware runs twice causing unexpected behavior.
DLaravel throws a 'Middleware [CheckAge] not found' error.
Attempts:
2 left
💡 Hint

Think about where Laravel looks for middleware when you use the middleware() method with a string.

📝 Syntax
intermediate
2:00remaining
Which option correctly registers a middleware named LogRequests for route usage?

You want to register a middleware LogRequests so you can use it in routes like ->middleware('log.requests'). Which registration in app/Http/Kernel.php is correct?

A'log.requests' => \App\Http\Middleware\LogRequests::class,
B'LogRequests' => \App\Http\Middleware\LogRequests::class,
C'logRequests' => \App\Http\Middleware\LogRequests::class,
D'log_requests' => \App\Http\Middleware\LogRequests::class,
Attempts:
2 left
💡 Hint

Middleware keys are case sensitive and usually use dot notation for readability.

🔧 Debug
advanced
2:00remaining
Why does this middleware not run on the route?

Given this Kernel snippet and route, why does the CheckUser middleware not execute?

Laravel
protected $middlewareGroups = [
    'api' => [
        \App\Http\Middleware\CheckUser::class,
    ],
];

Route::get('/data', function () {
    return 'Data';
})->middleware('CheckUser');
ABecause middleware groups cannot contain custom middleware.
BBecause 'CheckUser' is not registered in $routeMiddleware, Laravel cannot find it by name.
CBecause the route is missing the 'api' middleware group in its definition.
DBecause the middleware class has a syntax error and fails silently.
Attempts:
2 left
💡 Hint

Check where Laravel looks for middleware when you use a string in middleware().

state_output
advanced
2:00remaining
What is the output after registering middleware in $middleware vs $routeMiddleware?

Consider a middleware Authenticate registered in $middleware array and another middleware LogAccess registered in $routeMiddleware with key 'log.access'. If a route uses ->middleware('log.access'), what middleware run?

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

protected $routeMiddleware = [
    'log.access' => \App\Http\Middleware\LogAccess::class,
];

Route::get('/dashboard', function () {
    return 'Dashboard';
})->middleware('log.access');
AOnly LogAccess middleware runs; Authenticate is ignored.
BOnly Authenticate middleware runs; LogAccess is ignored.
CBoth Authenticate and LogAccess middleware run before the route returns 'Dashboard'.
DNeither middleware runs; the route returns 'Dashboard' directly.
Attempts:
2 left
💡 Hint

Think about global middleware vs route middleware.

🧠 Conceptual
expert
2:00remaining
Which statement about middleware registration and usage is TRUE?

Choose the correct statement about how Laravel middleware registration affects their usage in routes.

AMiddleware must be registered in $routeMiddleware to be used by string name in routes.
BMiddleware registered only in $middlewareGroups can be used by name in routes without errors.
CMiddleware registered in $middleware run only on routes that explicitly list them.
DMiddleware registered in $routeMiddleware run globally on all requests.
Attempts:
2 left
💡 Hint

Recall the difference between global, group, and route middleware.