📖 Scenario: You are building a Laravel web application that needs to check user roles before allowing access to certain pages.Middleware helps you run code before a request reaches your routes or controllers.
🎯 Goal: Learn how to register a custom middleware in Laravel and apply it to a route.
📋 What You'll Learn
Create a middleware class named CheckUserRole
Register the middleware in the app/Http/Kernel.php file
Apply the middleware to a route in routes/web.php
💡 Why This Matters
🌍 Real World
Middleware is used in real Laravel apps to control access, log requests, or modify responses before reaching controllers.
💼 Career
Understanding middleware registration and usage is essential for Laravel developers to implement security and request handling.
Progress0 / 4 steps
1
Create the CheckUserRole middleware class
Create a middleware class named CheckUserRole inside the app/Http/Middleware directory with a handle method that accepts $request and $next parameters.
Laravel
Hint
Use the handle method to pass the request to the next middleware or controller.
2
Register the CheckUserRole middleware in Kernel
Open the app/Http/Kernel.php file and add the CheckUserRole::class to the $routeMiddleware array with the key 'check.user.role'.
Laravel
Hint
Add the middleware to the $routeMiddleware array with a key you will use in routes.
3
Apply the middleware to a route
In the routes/web.php file, create a route for /dashboard that uses the check.user.role middleware.
Laravel
Hint
Use the middleware method on the route to apply your middleware key.
4
Complete the middleware to check user role
Update the handle method in CheckUserRole middleware to check if the authenticated user's role is 'admin'. If not, redirect to /home. Otherwise, allow the request to continue.
Laravel
Hint
Use Auth::check() to verify login and Auth::user()->role to check the role.
Practice
(1/5)
1. What is the main purpose of registering middleware in Laravel's Kernel.php file?
easy
A. To define database connections
B. To make middleware available for use in routes and controllers
C. To create new views
D. To register event listeners
Solution
Step 1: Understand middleware role
Middleware acts as a filter for HTTP requests in Laravel.
Step 2: Purpose of registering middleware
Registering middleware in Kernel.php makes it available to assign to routes or groups.
Final Answer:
To make middleware available for use in routes and controllers -> Option B
Quick Check:
Middleware registration = availability for routes [OK]
Hint: Middleware must be registered to use it in routes [OK]
Common Mistakes:
Confusing middleware registration with database setup
Thinking middleware creates views
Mixing middleware with event listeners
2. Which of the following is the correct way to register a route middleware named checkAge in app/Http/Kernel.php?
easy
A. 'checkAge' => \App\Http\Middleware\CheckAge::class,
B. 'checkAge' => 'CheckAgeMiddleware',
C. 'checkAge' => new CheckAge(),
D. 'checkAge' => 'middleware.checkAge',
Solution
Step 1: Locate middleware registration syntax
In Kernel.php, route middleware are registered as key-value pairs with the key as the name and value as the class reference.
Step 2: Match correct syntax
The correct syntax uses the fully qualified class name with ::class keyword.
Final Answer:
'checkAge' => \App\Http\Middleware\CheckAge::class, -> Option A
Quick Check:
Middleware registration uses class reference [OK]
Hint: Use full class path with ::class to register middleware [OK]
Common Mistakes:
Using string names instead of class references
Instantiating middleware instead of referencing class
Using incorrect keys or formats
3. Given the following Kernel.php snippet, what middleware will be applied when accessing the route /profile?
A. Middleware should be registered in $middleware property
B. Middleware name 'admin' is invalid
C. Missing the ::class keyword after the middleware class name
D. Namespace should not be used in Kernel.php
Solution
Step 1: Review middleware registration syntax
Middleware classes must be referenced with the ::class keyword to provide the full class name as a string.
Step 2: Identify missing part
The snippet misses ::class after App\Http\Middleware\AdminCheck.
Final Answer:
Missing the ::class keyword after the middleware class name -> Option C
Quick Check:
Middleware class references need ::class [OK]
Hint: Always add ::class after middleware class name [OK]
Common Mistakes:
Omitting ::class causing syntax errors
Registering middleware in wrong property
Using invalid middleware names
5. You want to create a middleware that logs every request to a specific route group. After creating the middleware class, what is the correct way to register and apply it to the route group in Laravel?
hard
A. Use the middleware class name as a string in the route group without registration
B. Add the middleware class directly inside the route group without registration
C. Register the middleware in $middleware property and apply it only to the route group
D. Register the middleware in Kernel.php under $routeMiddleware with a name, then apply it in the route group using middleware('name')
Solution
Step 1: Register middleware properly
Custom middleware must be registered in $routeMiddleware in Kernel.php with a key name.
Step 2: Apply middleware to route group
Use the registered name in the route group with middleware('name') to apply it.
Final Answer:
Register the middleware in Kernel.php under $routeMiddleware with a name, then apply it in the route group using middleware('name') -> Option D
Quick Check:
Register then apply by name for route groups [OK]
Hint: Register middleware name, then use it in route groups [OK]
Common Mistakes:
Skipping registration and using class name directly
Registering middleware globally instead of route-specific
Misplacing middleware registration property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.