Global middleware runs on every HTTP request to your application. It applies to all routes automatically without needing to be assigned individually.
Click to reveal answer
beginner
Where do you register global middleware in a Laravel app?
You register global middleware in the app/Http/Kernel.php file inside the $middleware array.
Click to reveal answer
intermediate
How does global middleware differ from route middleware?
Global middleware runs on every request automatically. Route middleware runs only on routes or groups where you assign it explicitly.
Click to reveal answer
beginner
Give an example of a common global middleware in Laravel.
The CheckForMaintenanceMode middleware is often global. It checks if the app is in maintenance mode and blocks requests accordingly.
Click to reveal answer
intermediate
Why use global middleware instead of route middleware?
Use global middleware for tasks that must happen on every request, like security checks or trimming input. It ensures consistency without repeating assignments.
Click to reveal answer
Where do you add global middleware in Laravel?
AIn the $middleware array of app/Http/Kernel.php
BIn the routes/web.php file
CIn the config/app.php file
DIn the middleware folder only
✗ Incorrect
Global middleware is registered in the $middleware array inside app/Http/Kernel.php.
What happens if you add middleware to the $middleware array?
AIt runs only on web routes
BIt runs only on API routes
CIt runs on every HTTP request
DIt runs only when assigned to a route
✗ Incorrect
Middleware in the $middleware array runs globally on every HTTP request.
Which middleware type requires manual assignment to routes?
AGlobal middleware
BRoute middleware
CKernel middleware
DAutomatic middleware
✗ Incorrect
Route middleware must be assigned to routes or route groups explicitly.
Why might you avoid putting heavy tasks in global middleware?
ABecause it runs on every request and can slow down the app
BBecause it only runs once
CBecause it does not run on API routes
DBecause it is not secure
✗ Incorrect
Global middleware runs on every request, so heavy tasks can slow down all requests.
Which of these is a typical use for global middleware?
AAssigning roles to users
BLogging user activity only on admin pages
CApplying CSRF protection only on forms
DChecking maintenance mode
✗ Incorrect
Checking maintenance mode is a common global middleware task because it applies to all requests.
Explain what global middleware is in Laravel and how it is registered.
Think about middleware that always runs no matter the route.
You got /3 concepts.
Describe the difference between global middleware and route middleware in Laravel.
Consider when each middleware type runs during a request.
You got /4 concepts.
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
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 A
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
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 C
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