Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel middleware?
Consider this middleware that checks if a user is an admin before allowing access. What will happen if a non-admin user tries to access a protected route?
Laravel
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class CheckAdmin { public function handle(Request $request, Closure $next) { if (!$request->user() || !$request->user()->is_admin) { return redirect('/home'); } return $next($request); } }
Attempts:
2 left
💡 Hint
Look at the condition inside the handle method and what happens when it fails.
✗ Incorrect
The middleware checks if the user is logged in and has the 'is_admin' flag. If not, it redirects to '/home'. Otherwise, it allows the request to continue.
📝 Syntax
intermediate1:30remaining
Which middleware registration is correct in Laravel?
You want to register a middleware named 'CheckAge' to be used in routes. Which option correctly registers it in the HTTP kernel?
Attempts:
2 left
💡 Hint
Middleware classes are registered as class names, not instances or strings without ::class.
✗ Incorrect
Middleware must be registered as a key pointing to the fully qualified class name with ::class. Option B follows this pattern.
🔧 Debug
advanced2:30remaining
Why does this middleware cause a 'Too many redirects' error?
This middleware redirects users to '/login' if they are not authenticated. However, users get stuck in a redirect loop. What is the cause?
Laravel
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class RedirectIfNotAuthenticated { public function handle(Request $request, Closure $next) { if (!$request->user()) { return redirect('/login'); } return $next($request); } }
Attempts:
2 left
💡 Hint
Think about what happens when the user is already on the '/login' page.
✗ Incorrect
The middleware redirects any unauthenticated user to '/login', including requests already targeting '/login'. This causes an infinite redirect loop.
❓ state_output
advanced2:00remaining
What is the value of $request->header('X-Custom') after this middleware runs?
This middleware adds a custom header to the request before passing it on. What will be the value of the 'X-Custom' header in the next middleware or controller?
Laravel
<?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; class AddCustomHeader { public function handle(Request $request, Closure $next) { $request->headers->set('X-Custom', '12345'); return $next($request); } }
Attempts:
2 left
💡 Hint
Headers can be modified on the request object before passing it on.
✗ Incorrect
The middleware sets the 'X-Custom' header to '12345'. This header will be available in the next middleware or controller.
🧠 Conceptual
expert1:30remaining
Which statement about Laravel middleware is true?
Choose the correct statement about how Laravel middleware works.
Attempts:
2 left
💡 Hint
Think about the middleware handle method and the $next callback.
✗ Incorrect
Middleware wraps the request handling process, allowing modification of both request and response. It can stop requests or modify responses.