What if you could add one rule that works everywhere without repeating yourself?
Why Global middleware in Laravel? - Purpose & Use Cases
Imagine you have to check if every user is logged in before they access any page on your website. You try to add this check inside every single page's code manually.
Manually adding the same login check everywhere is tiring, easy to forget, and if you want to change the check later, you must update every page. This wastes time and causes bugs.
Global middleware lets you write the login check once and automatically runs it before every page request. This keeps your code clean and consistent without repeating yourself.
if (!userIsLoggedIn()) { redirectToLogin(); } // repeated in every controller method
// Define global middleware that runs before all requests class CheckLogin { public function handle($request, $next) { if (!userIsLoggedIn()) { return redirect('login'); } return $next($request); } }
Global middleware makes it easy to apply rules or actions across your whole app with one simple setup.
Every time someone visits your site, global middleware can check if they are logged in and redirect them to login if not, without writing that logic on every page.
Manual checks everywhere cause repeated code and bugs.
Global middleware runs code before every request automatically.
This keeps your app secure and your code clean.
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
