📖 Scenario: You are building a simple Laravel web application that has a controller to manage user profiles. You want to protect the profile routes so that only authenticated users can access them.
🎯 Goal: Create a controller called UserProfileController and apply the built-in auth middleware to it. This will ensure only logged-in users can access the controller's methods.
📋 What You'll Learn
Create a controller named UserProfileController
Add the auth middleware to the controller's constructor
Create a method show that returns a simple string
Ensure the middleware protects all methods in the controller
💡 Why This Matters
🌍 Real World
Middleware is commonly used in Laravel to protect routes and controllers, ensuring only authorized users can access certain parts of a web application.
💼 Career
Understanding how to apply middleware in Laravel controllers is essential for backend developers working with Laravel to build secure web applications.
Progress0 / 4 steps
1
Create the UserProfileController
Create a controller class named UserProfileController inside the App\Http\Controllers namespace. Add a public method called show that returns the string 'User Profile Page'.
Laravel
Hint
Remember to define the class inside the correct namespace and add the show method that returns the exact string.
2
Add the auth middleware in the constructor
Inside the UserProfileController class, add a constructor method __construct. In this constructor, apply the auth middleware using $this->middleware('auth');.
Laravel
Hint
The constructor method is named __construct. Use $this->middleware('auth'); inside it to apply the middleware.
3
Add a second method to demonstrate middleware protection
Add a public method named edit inside UserProfileController that returns the string 'Edit User Profile'. This method will also be protected by the auth middleware.
Laravel
Hint
Define the edit method similar to show, returning the exact string.
4
Complete the controller with middleware protecting all methods
Ensure the UserProfileController class has the auth middleware applied in the constructor and includes both show and edit methods returning their respective strings. This completes the controller setup with middleware protection.
Laravel
Hint
Review the controller to confirm the middleware is applied and both methods are present with correct return values.
Practice
(1/5)
1. What is the main purpose of middleware in a Laravel controller?
easy
A. To connect the controller to the database
B. To define routes for the controller
C. To run code before or after controller methods for access control or modification
D. To create views for the controller
Solution
Step 1: Understand middleware role
Middleware acts as a filter that runs before or after controller methods to control access or modify requests/responses.
Step 2: Differentiate from other controller tasks
Defining routes, connecting to databases, or creating views are not middleware responsibilities.
Final Answer:
To run code before or after controller methods for access control or modification -> Option C
Quick Check:
Middleware controls access and actions around controller methods = A [OK]
Thinking multiple middleware calls override each other
Assuming except() needs array only
Believing syntax causes error
5. You want to apply the 'verified' middleware to all controller methods except 'index' and 'show', and also apply 'log' middleware only to 'store' and 'update'. Which constructor code correctly implements this using arrays?
hard
A. public function __construct() {
$this->middleware('verified')->only('index', 'show');
$this->middleware('log')->except('store', 'update');
}
B. public function __construct() {
$this->middleware('verified')->except(['index', 'show']);
$this->middleware('log')->only(['store', 'update']);
}
C. public function __construct() {
$this->middleware('verified');
$this->middleware('log');
}
D. public function __construct() {
$this->middleware('verified')->except('index', 'show');
$this->middleware('log')->only('store', 'update');
}
Solution
Step 1: Apply 'verified' middleware except on 'index' and 'show'
Use except() with array to exclude 'index' and 'show' from 'verified' middleware.
Step 2: Apply 'log' middleware only on 'store' and 'update'
Use only() with array to limit 'log' middleware to 'store' and 'update' methods.
Step 3: Compare options
public function __construct() {
$this->middleware('verified')->except('index', 'show');
$this->middleware('log')->only('store', 'update');
} uses except() and only() with multiple string arguments (valid but less common). public function __construct() {
$this->middleware('verified')->except(['index', 'show']);
$this->middleware('log')->only(['store', 'update']);
} uses arrays which is clearer and recommended. Options B and C do not match requirements.
Final Answer:
public function __construct() {
$this->middleware('verified')->except(['index', 'show']);
$this->middleware('log')->only(['store', 'update']);
} -> Option B
Quick Check:
Use except() and only() with arrays for clarity = A [OK]
Hint: Use except() and only() with arrays for multiple methods [OK]
Common Mistakes:
Mixing only() and except() incorrectly
Not using arrays for multiple methods
Applying middleware to wrong methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.