Global middleware runs on every request to your Laravel app. It helps you add common checks or actions for all users.
Global middleware in Laravel
protected $middleware = [
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];This code goes inside the app/Http/Kernel.php file.
Each middleware class listed here runs on every HTTP request.
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
];protected $middleware = [
\App\Http\Middleware\TrimStrings::class,
\App\Http\Middleware\ConvertEmptyStringsToNull::class,
];This middleware adds a custom header to every HTTP response. Adding it to the global middleware list makes it run on all requests.
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class AddCustomHeader { public function handle(Request $request, Closure $next) { $response = $next($request); $response->headers->set('X-Custom-Header', 'HelloWorld'); return $response; } } // Then in app/Http/Kernel.php add: // protected $middleware = [ // \App\Http\Middleware\AddCustomHeader::class, // // other middleware... // ];
Global middleware affects every request, so keep them efficient to avoid slowing your app.
You can still use route middleware for specific routes if you want to run middleware only sometimes.
Remember to register your custom middleware class in app/Http/Kernel.php under the $middleware array.
Global middleware runs on every request to your Laravel app.
It is defined in the $middleware array inside app/Http/Kernel.php.
Use it for tasks like authentication checks, logging, or modifying all responses.
Practice
Solution
Step 1: Understand what middleware does
Middleware acts as a filter or layer that runs code before or after a request is handled.Step 2: Identify the role of global middleware
Global middleware runs on every HTTP request, applying tasks like authentication or logging universally.Final Answer:
To run code on every HTTP request to the application -> Option AQuick Check:
Global middleware = runs on every request [OK]
- Confusing middleware with routing
- Thinking middleware only runs on login
- Assuming middleware manages database
Solution
Step 1: Recall where middleware is registered
Middleware is registered in the Kernel class which manages HTTP middleware stacks.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.Final Answer:
app/Http/Kernel.php -> Option CQuick Check:
Global middleware registered in Kernel.php [OK]
- Choosing routes/web.php which defines routes only
- Confusing config/app.php with middleware registration
- Selecting service providers which are for services
app/Http/Kernel.php:
$middleware = [
\App\Http\Middleware\CheckAge::class,
];
What happens when a user sends a request to the app?Solution
Step 1: Understand the $middleware array role
The $middleware array in Kernel.php lists global middleware that runs on every request.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.Final Answer:
The CheckAge middleware runs on every request -> Option BQuick Check:
$middleware = global middleware runs always [OK]
- Thinking middleware runs only on certain HTTP methods
- Assuming middleware runs only on specific routes
- Believing middleware must be manually attached to routes
$middleware in app/Http/Kernel.php, but it does not run on requests. What is the most likely cause?Solution
Step 1: Understand difference between $middleware and $routeMiddleware
$middleware is for global middleware; $routeMiddleware is for middleware assigned to specific routes.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.Final Answer:
The middleware class is registered in $routeMiddleware instead of $middleware -> Option AQuick Check:
Global middleware must be in $middleware array [OK]
- 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
Solution
Step 1: Identify how to apply logging globally
Global middleware runs on every request, perfect for logging all requests centrally.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.Step 3: Evaluate other options
Logging in routes or controllers is repetitive and error-prone; assigning route middleware to all routes is inefficient.Final Answer:
Create a middleware with a handle method that logs request info and add it to $middleware in Kernel.php -> Option DQuick Check:
Global logging = global middleware in $middleware [OK]
- Logging inside routes causes duplication
- Controller constructors don't cover all requests
- Assigning route middleware manually is tedious and error-prone
Ready to prove it?
Pass the test and this pattern moves unknown
