0
0
Laravelframework~20 mins

Creating middleware in Laravel - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
    }
}
AThe middleware throws a server error.
BThe user sees a 404 error page.
CThe user is allowed to proceed regardless of admin status.
DThe user is redirected to '/home' if not admin.
Attempts:
2 left
💡 Hint
Look at the condition inside the handle method and what happens when it fails.
📝 Syntax
intermediate
1: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?
A'checkAge' => \App\Http\Middleware\CheckAge(),
B'check.age' => \App\Http\Middleware\CheckAge::class,
C'check_age' => new \App\Http\Middleware\CheckAge,
D'check-age' => 'App\Http\Middleware\CheckAge',
Attempts:
2 left
💡 Hint
Middleware classes are registered as class names, not instances or strings without ::class.
🔧 Debug
advanced
2: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);
    }
}
AThe middleware redirects '/login' requests back to '/login' causing a loop.
BThe middleware does not check if the user is authenticated correctly.
CThe middleware should use abort(401) instead of redirect.
DThe middleware is missing a return statement after redirect.
Attempts:
2 left
💡 Hint
Think about what happens when the user is already on the '/login' page.
state_output
advanced
2: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);
    }
}
AThrows an error because headers are immutable
Bnull
C'12345'
D'x-custom'
Attempts:
2 left
💡 Hint
Headers can be modified on the request object before passing it on.
🧠 Conceptual
expert
1:30remaining
Which statement about Laravel middleware is true?
Choose the correct statement about how Laravel middleware works.
AMiddleware can modify the request before passing it to the controller and the response before sending it back.
BMiddleware only runs after the controller has processed the request.
CMiddleware cannot stop a request from reaching the controller once started.
DMiddleware must always return a JSON response.
Attempts:
2 left
💡 Hint
Think about the middleware handle method and the $next callback.