Bird
Raised Fist0
Laravelframework~20 mins

Registering middleware in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of registering middleware in Laravel's Kernel.php file?
easy
A. To define database connections
B. To make middleware available for use in routes and controllers
C. To create new views
D. To register event listeners

Solution

  1. Step 1: Understand middleware role

    Middleware acts as a filter for HTTP requests in Laravel.
  2. Step 2: Purpose of registering middleware

    Registering middleware in Kernel.php makes it available to assign to routes or groups.
  3. Final Answer:

    To make middleware available for use in routes and controllers -> Option B
  4. Quick Check:

    Middleware registration = availability for routes [OK]
Hint: Middleware must be registered to use it in routes [OK]
Common Mistakes:
  • Confusing middleware registration with database setup
  • Thinking middleware creates views
  • Mixing middleware with event listeners
2. Which of the following is the correct way to register a route middleware named checkAge in app/Http/Kernel.php?
easy
A. 'checkAge' => \App\Http\Middleware\CheckAge::class,
B. 'checkAge' => 'CheckAgeMiddleware',
C. 'checkAge' => new CheckAge(),
D. 'checkAge' => 'middleware.checkAge',

Solution

  1. Step 1: Locate middleware registration syntax

    In Kernel.php, route middleware are registered as key-value pairs with the key as the name and value as the class reference.
  2. Step 2: Match correct syntax

    The correct syntax uses the fully qualified class name with ::class keyword.
  3. Final Answer:

    'checkAge' => \App\Http\Middleware\CheckAge::class, -> Option A
  4. Quick Check:

    Middleware registration uses class reference [OK]
Hint: Use full class path with ::class to register middleware [OK]
Common Mistakes:
  • Using string names instead of class references
  • Instantiating middleware instead of referencing class
  • Using incorrect keys or formats
3. Given the following Kernel.php snippet, what middleware will be applied when accessing the route /profile?
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'verified' => \App\Http\Middleware\EnsureEmailIsVerified::class,
];

Route::middleware(['auth', 'verified'])->get('/profile', function () {
    return 'User Profile';
});
medium
A. Both 'auth' and 'verified' middleware run
B. Only the 'auth' middleware runs
C. No middleware runs
D. Only the 'verified' middleware runs

Solution

  1. Step 1: Check middleware registration

    Both 'auth' and 'verified' middleware are registered in $routeMiddleware.
  2. Step 2: Analyze route middleware assignment

    The route applies both middleware by listing them in the array ['auth', 'verified'].
  3. Final Answer:

    Both 'auth' and 'verified' middleware run -> Option A
  4. Quick Check:

    Route middleware array applies all listed middleware [OK]
Hint: All middleware in array run in order [OK]
Common Mistakes:
  • Assuming only one middleware runs
  • Confusing middleware registration with application
  • Ignoring middleware order
4. Identify the error in this middleware registration snippet inside Kernel.php:
protected $routeMiddleware = [
    'admin' => App\Http\Middleware\AdminCheck,
];
medium
A. Middleware should be registered in $middleware property
B. Middleware name 'admin' is invalid
C. Missing the ::class keyword after the middleware class name
D. Namespace should not be used in Kernel.php

Solution

  1. Step 1: Review middleware registration syntax

    Middleware classes must be referenced with the ::class keyword to provide the full class name as a string.
  2. Step 2: Identify missing part

    The snippet misses ::class after App\Http\Middleware\AdminCheck.
  3. Final Answer:

    Missing the ::class keyword after the middleware class name -> Option C
  4. Quick Check:

    Middleware class references need ::class [OK]
Hint: Always add ::class after middleware class name [OK]
Common Mistakes:
  • Omitting ::class causing syntax errors
  • Registering middleware in wrong property
  • Using invalid middleware names
5. You want to create a middleware that logs every request to a specific route group. After creating the middleware class, what is the correct way to register and apply it to the route group in Laravel?
hard
A. Use the middleware class name as a string in the route group without registration
B. Add the middleware class directly inside the route group without registration
C. Register the middleware in $middleware property and apply it only to the route group
D. Register the middleware in Kernel.php under $routeMiddleware with a name, then apply it in the route group using middleware('name')

Solution

  1. Step 1: Register middleware properly

    Custom middleware must be registered in $routeMiddleware in Kernel.php with a key name.
  2. Step 2: Apply middleware to route group

    Use the registered name in the route group with middleware('name') to apply it.
  3. Final Answer:

    Register the middleware in Kernel.php under $routeMiddleware with a name, then apply it in the route group using middleware('name') -> Option D
  4. Quick Check:

    Register then apply by name for route groups [OK]
Hint: Register middleware name, then use it in route groups [OK]
Common Mistakes:
  • Skipping registration and using class name directly
  • Registering middleware globally instead of route-specific
  • Misplacing middleware registration property
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard