Bird
Raised Fist0
Laravelframework~10 mins

Global middleware in Laravel - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to register a global middleware in Laravel's HTTP kernel.

Laravel
protected $middleware = [
    \App\Http\Middleware\TrustProxies::class,
    [1],
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\Authenticate::class
B\App\Http\Middleware\RedirectIfAuthenticated::class
C\App\Http\Middleware\CheckForMaintenanceMode::class
D\App\Http\Middleware\VerifyCsrfToken::class
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing middleware meant for route groups instead of global middleware.
Confusing authentication middleware with maintenance mode middleware.
2fill in blank
medium

Complete the code to add a custom global middleware class named LogRequests.

Laravel
protected $middleware = [
    [1],
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\VerifyCsrfToken::class
B\App\Http\Middleware\Authenticate::class
C\App\Http\Middleware\RedirectIfAuthenticated::class
D\App\Http\Middleware\LogRequests::class
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to add the ::class suffix.
Adding middleware to the wrong property like $routeMiddleware.
3fill in blank
hard

Fix the error in the middleware registration by completing the missing part.

Laravel
protected $middleware = [
    \App\Http\Middleware\[1]::class,
];
Drag options to blanks, or click blank then click option'
ATrustProxies
BLogRequests
CAuthenticate
DRedirectIfAuthenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing middleware that is usually route-specific instead of global.
Using middleware names that do not exist in the default Laravel setup.
4fill in blank
hard

Fill both blanks to correctly add two global middleware classes: CheckForMaintenanceMode and ValidatePostSize.

Laravel
protected $middleware = [
    [1],
    [2],
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\CheckForMaintenanceMode::class
B\App\Http\Middleware\Authenticate::class
C\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class
D\App\Http\Middleware\RedirectIfAuthenticated::class
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing route middleware with global middleware.
Forgetting to include the full namespace.
5fill in blank
hard

Fill all three blanks to create a global middleware array with TrustProxies, CheckForMaintenanceMode, and ValidatePostSize.

Laravel
protected $middleware = [
    [1],
    [2],
    [3],
];
Drag options to blanks, or click blank then click option'
A\App\Http\Middleware\Authenticate::class
B\App\Http\Middleware\CheckForMaintenanceMode::class
C\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class
D\App\Http\Middleware\TrustProxies::class
Attempts:
3 left
💡 Hint
Common Mistakes
Using middleware classes that are not global.
Incorrect namespace or missing ::class.

Practice

(1/5)
1. What is the main purpose of global middleware in a Laravel application?
easy
A. To run code on every HTTP request to the application
B. To handle database migrations automatically
C. To define routes for the application
D. To manage user sessions only on login

Solution

  1. Step 1: Understand what middleware does

    Middleware acts as a filter or layer that runs code before or after a request is handled.
  2. Step 2: Identify the role of global middleware

    Global middleware runs on every HTTP request, applying tasks like authentication or logging universally.
  3. Final Answer:

    To run code on every HTTP request to the application -> Option A
  4. Quick Check:

    Global middleware = runs on every request [OK]
Hint: Global middleware affects all requests, not just some [OK]
Common Mistakes:
  • Confusing middleware with routing
  • Thinking middleware only runs on login
  • Assuming middleware manages database
2. Which file in a Laravel project is used to register global middleware?
easy
A. config/app.php
B. routes/web.php
C. app/Http/Kernel.php
D. app/Providers/AppServiceProvider.php

Solution

  1. Step 1: Recall where middleware is registered

    Middleware is registered in the Kernel class which manages HTTP middleware stacks.
  2. Step 2: Identify the correct Kernel file

    In Laravel, the HTTP Kernel is located at app/Http/Kernel.php where global middleware is listed in the $middleware array.
  3. Final Answer:

    app/Http/Kernel.php -> Option C
  4. Quick Check:

    Global middleware registered in Kernel.php [OK]
Hint: Middleware registration is in Kernel.php, not routes or config [OK]
Common Mistakes:
  • Choosing routes/web.php which defines routes only
  • Confusing config/app.php with middleware registration
  • Selecting service providers which are for services
3. Given this snippet in app/Http/Kernel.php:
$middleware = [
    \App\Http\Middleware\CheckAge::class,
];
What happens when a user sends a request to the app?
medium
A. The CheckAge middleware runs only on POST requests
B. The CheckAge middleware runs on every request
C. The CheckAge middleware runs only on routes with 'age' prefix
D. The CheckAge middleware does not run automatically

Solution

  1. Step 1: Understand the $middleware array role

    The $middleware array in Kernel.php lists global middleware that runs on every request.
  2. Step 2: Analyze the effect of adding CheckAge middleware there

    Since CheckAge is in $middleware, it will execute on all HTTP requests regardless of method or route.
  3. Final Answer:

    The CheckAge middleware runs on every request -> Option B
  4. Quick Check:

    $middleware = global middleware runs always [OK]
Hint: Middleware in $middleware runs on all requests, no exceptions [OK]
Common Mistakes:
  • Thinking middleware runs only on certain HTTP methods
  • Assuming middleware runs only on specific routes
  • Believing middleware must be manually attached to routes
4. You added a new global middleware class to $middleware in app/Http/Kernel.php, but it does not run on requests. What is the most likely cause?
medium
A. The middleware class is registered in $routeMiddleware instead of $middleware
B. The middleware class is not imported in routes/web.php
C. The middleware class is missing a constructor
D. The middleware class does not implement the handle method

Solution

  1. Step 1: Understand difference between $middleware and $routeMiddleware

    $middleware is for global middleware; $routeMiddleware is for middleware assigned to specific routes.
  2. Step 2: Identify why middleware won't run globally

    If middleware is only registered in $routeMiddleware, it won't run on every request unless assigned to routes.
  3. Final Answer:

    The middleware class is registered in $routeMiddleware instead of $middleware -> Option A
  4. Quick Check:

    Global middleware must be in $middleware array [OK]
Hint: Global middleware must be in $middleware, not $routeMiddleware [OK]
Common Mistakes:
  • Forgetting to implement handle method causes errors, not silent skip
  • Importing middleware in routes/web.php is unnecessary
  • Assuming constructor is required for middleware to run
5. You want to log every request's URL and method globally in your Laravel app. Which is the best way to do this using global middleware?
hard
A. Add logging code directly inside routes/web.php for each route
B. Register a route middleware and assign it to all routes manually
C. Use a controller constructor to log requests for each controller
D. Create a middleware with a handle method that logs request info and add it to $middleware in Kernel.php

Solution

  1. Step 1: Identify how to apply logging globally

    Global middleware runs on every request, perfect for logging all requests centrally.
  2. Step 2: Implement middleware and register globally

    Create middleware with a handle method that logs request URL and method, then add it to $middleware array in Kernel.php.
  3. Step 3: Evaluate other options

    Logging in routes or controllers is repetitive and error-prone; assigning route middleware to all routes is inefficient.
  4. Final Answer:

    Create a middleware with a handle method that logs request info and add it to $middleware in Kernel.php -> Option D
  5. Quick Check:

    Global logging = global middleware in $middleware [OK]
Hint: Use global middleware for app-wide request logging [OK]
Common Mistakes:
  • Logging inside routes causes duplication
  • Controller constructors don't cover all requests
  • Assigning route middleware manually is tedious and error-prone
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