0
0
Laravelframework~5 mins

Creating middleware in Laravel - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What is middleware in Laravel?
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
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>
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
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
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
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.