Middleware is a way to filter HTTP requests entering your application. It acts like a gatekeeper that can inspect, modify, or reject requests before they reach your routes or controllers.
Click to reveal answer
beginner
How do you create a new middleware in Laravel?
You use the artisan command: <code>php artisan make:middleware MiddlewareName</code>. This creates a new middleware class in the <code>app/Http/Middleware</code> directory.
Click to reveal answer
beginner
What method must you implement in a Laravel middleware class?
You must implement the handle method. It receives the request and a closure to pass the request to the next middleware or controller.
Click to reveal answer
intermediate
How do you register middleware so Laravel uses it?
You register middleware in app/Http/Kernel.php. You can add it to the $middleware array for global use or to $routeMiddleware for specific routes.
Click to reveal answer
beginner
How do you apply middleware to a route in Laravel?
You apply middleware by adding the middleware method to a route or group, like Route::get('/home', ...)->middleware('auth');.
Click to reveal answer
Which artisan command creates a new middleware in Laravel?
Aphp artisan make:middleware MiddlewareName
Bphp artisan create:middleware MiddlewareName
Cphp artisan new:middleware MiddlewareName
Dphp artisan generate:middleware MiddlewareName
✗ Incorrect
The correct command is php artisan make:middleware MiddlewareName.
Where do you register route-specific middleware in Laravel?
AIn <code>app/Http/Kernel.php</code> inside <code>$routeMiddleware</code>
BIn <code>routes/web.php</code>
CIn <code>config/middleware.php</code>
DIn <code>app/Providers/AppServiceProvider.php</code>
✗ Incorrect
Route-specific middleware is registered in app/Http/Kernel.php inside the $routeMiddleware array.
What does the handle method in middleware do?
AGenerates views for the user
BDefines routes for the application
CHandles database migrations
DProcesses the request and passes it to the next middleware or controller
✗ Incorrect
The handle method processes the incoming request and decides whether to pass it along or reject it.
How do you apply middleware to a route in Laravel?
ABy adding middleware in the controller constructor
BBy editing the <code>config/app.php</code> file
CUsing the <code>middleware</code> method on the route
DBy creating a new route group without middleware
✗ Incorrect
Middleware is applied to routes using the middleware method, e.g., Route::get(...)->middleware('auth').
What is the main purpose of middleware in Laravel?
ATo generate HTML views
BTo filter and handle HTTP requests before they reach controllers
CTo create database tables
DTo manage user sessions only
✗ Incorrect
Middleware filters HTTP requests and can modify or block them before they reach the controller.
Explain how to create and register a middleware in Laravel and how to apply it to a route.
Think about the steps from creation to usage in routes.
You got /4 concepts.
Describe the role of the handle method inside a Laravel middleware class.
Focus on what happens inside the handle method.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of middleware in Laravel?
easy
A. To define routes for the application
B. To filter and modify HTTP requests before they reach the controller
C. To create database migrations automatically
D. To generate views for the user interface
Solution
Step 1: Understand middleware role
Middleware acts as a filter that processes HTTP requests before they reach the controller or after the response leaves the controller.
Step 2: Identify correct purpose
Among the options, only filtering and modifying HTTP requests matches middleware's role.
Final Answer:
To filter and modify HTTP requests before they reach the controller -> Option B
Quick Check:
Middleware filters requests [OK]
Hint: Middleware filters requests before controllers [OK]
Common Mistakes:
Confusing middleware with routing
Thinking middleware creates database migrations
Assuming middleware generates views
2. Which command correctly creates a new middleware named CheckAge in Laravel?
easy
A. php artisan make:middleware CheckAge
B. php artisan create:middleware CheckAge
C. php artisan middleware:create CheckAge
D. php artisan generate:middleware CheckAge
Solution
Step 1: Recall Laravel artisan command for middleware
The correct command to create middleware is php artisan make:middleware Name.
Step 2: Match the command with options
Only php artisan make:middleware CheckAge uses the exact correct syntax.
Final Answer:
php artisan make:middleware CheckAge -> Option A
Quick Check:
Use 'make:middleware' to create middleware [OK]
Hint: Use 'make:middleware' with artisan to create middleware [OK]
Common Mistakes:
Using 'create:middleware' instead of 'make:middleware'
Mixing command order or keywords
Forgetting to specify middleware name
3. Given this middleware handle method, what will happen if the user is not authenticated?
public function handle($request, Closure $next) {
if (!auth()->check()) {
return redirect('login');
}
return $next($request);
}
medium
A. The request proceeds to the next middleware or controller
B. The request is blocked with a 403 Forbidden response
C. An error is thrown because of missing return
D. The user is redirected to the login page
Solution
Step 1: Analyze the authentication check
The code checks if the user is not authenticated using !auth()->check().
Step 2: Understand the redirect action
If not authenticated, it returns a redirect response to 'login', stopping further request processing.
Final Answer:
The user is redirected to the login page -> Option D
Quick Check:
Unauthenticated users get redirected [OK]
Hint: Redirect happens if auth check fails [OK]
Common Mistakes:
Assuming request proceeds despite failed auth
Thinking an error is thrown
Confusing redirect with 403 response
4. Identify the error in this middleware handle method:
public function handle($request, Closure $next) {
if ($request->user()->role !== 'admin') {
abort(403, 'Unauthorized');
}
}
medium
A. No error, this middleware is correct
B. Incorrect method name, should be handleRequest
C. Missing return statement for $next middleware call
D. Using abort() is not allowed in middleware
Solution
Step 1: Check middleware handle method structure
Middleware must return either a response or call $next($request) to continue the request.
Step 2: Identify missing return
This code aborts if role is not admin but does not return $next($request) if the user is admin, causing no response to be returned.
Final Answer:
Missing return statement for $next middleware call -> Option C
Quick Check:
Always return $next($request) to continue [OK]
Hint: Always return $next($request) in middleware [OK]
Common Mistakes:
Forgetting to return $next($request)
Thinking abort() is disallowed
Renaming handle method incorrectly
5. You want to create middleware that blocks requests with a query parameter blocked=true by aborting with a 403 Forbidden response. Which handle method implementation correctly achieves this?
hard
A. public function handle($request, Closure $next) {
if ($request->query('blocked') === 'true') {
abort(403, 'Access denied');
}
return $next($request);
}
B. public function handle($request, Closure $next) {
if ($request->input('blocked') == true) {
return redirect('home');
}
return $next($request);
}
C. public function handle($request, Closure $next) {
if ($request->blocked === true) {
abort(404);
}
return $next($request);
}
D. public function handle($request, Closure $next) {
if ($request->has('blocked')) {
return response('Blocked', 401);
}
return $next($request);
}
Solution
Step 1: Check how to read query parameters
Use $request->query('blocked') to get the query parameter value as string.
Step 2: Verify condition and response
public function handle($request, Closure $next) {
if ($request->query('blocked') === 'true') {
abort(403, 'Access denied');
}
return $next($request);
} checks if 'blocked' equals string 'true' and aborts with 403, then returns next middleware call.
Step 3: Compare other options
public function handle($request, Closure $next) {
if ($request->input('blocked') == true) {
return redirect('home');
}
return $next($request);
} uses input() and redirects instead of aborting; public function handle($request, Closure $next) {
if ($request->blocked === true) {
abort(404);
}
return $next($request);
} accesses property incorrectly; public function handle($request, Closure $next) {
if ($request->has('blocked')) {
return response('Blocked', 401);
}
return $next($request);
} returns 401 but blocks any 'blocked' param regardless of value.
Final Answer:
Checks $request->query('blocked') === 'true' and uses abort(403, 'Access denied') -> Option A
Quick Check:
Use $request->query('blocked') and abort(403) [OK]
Hint: Use $request->query('param') and abort(403) to block [OK]
Common Mistakes:
Using $request->blocked instead of query()
Returning redirect instead of abort
Not checking exact 'true' string value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.