Challenge - 5 Problems
Controller Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when middleware is applied to a Laravel controller?
Consider a Laravel controller with middleware applied in its constructor. What is the effect of this middleware on the controller's methods?
Laravel
class PostController extends Controller { public function __construct() { $this->middleware('auth'); } public function index() { return 'Posts list'; } public function show($id) { return "Post $id"; } }
Attempts:
2 left
💡 Hint
Think about when middleware runs in the request lifecycle.
✗ Incorrect
Middleware applied in the controller constructor runs before any controller method. It protects all methods unless exceptions are specified.
📝 Syntax
intermediate2:00remaining
Which code correctly applies middleware only to specific controller methods?
You want to apply the 'auth' middleware only to the 'edit' and 'update' methods of a Laravel controller. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Check the correct method chaining syntax for middleware restrictions.
✗ Incorrect
The only method accepts an array of method names to apply middleware to. Option A uses the correct syntax.
🔧 Debug
advanced2:00remaining
Why does this middleware not run on the 'show' method?
Given this controller code, why does the 'show' method not require authentication?
Laravel
class ArticleController extends Controller { public function __construct() { $this->middleware('auth')->except('show'); } public function show($id) { return "Article $id"; } public function edit($id) { return "Edit Article $id"; } }
Attempts:
2 left
💡 Hint
Look at the except method usage in middleware.
✗ Incorrect
The except method excludes specified methods from middleware. Here, 'show' is excluded, so no auth check runs for it.
❓ state_output
advanced2:00remaining
What is the output when middleware aborts the request?
If the 'auth' middleware aborts the request with a 403 error, what will the user see when accessing a protected controller method?
Laravel
Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('auth'); class DashboardController extends Controller { public function index() { return 'Welcome to dashboard'; } }
Attempts:
2 left
💡 Hint
Middleware can stop the request before the controller runs.
✗ Incorrect
Middleware runs before the controller method. If it aborts with 403, the controller method is never called and the error page is shown.
🧠 Conceptual
expert2:00remaining
How does Laravel handle multiple middleware on a controller?
If a Laravel controller applies multiple middleware in its constructor like this:
$this->middleware('auth');
$this->middleware('log');
In what order do these middleware run when a request hits a controller method?
Attempts:
2 left
💡 Hint
Think about the middleware stack order in Laravel.
✗ Incorrect
Laravel runs middleware in the order they are registered. The first middleware added runs first.