0
0
Laravelframework~20 mins

Why middleware filters requests in Laravel - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why does middleware filter requests in Laravel?
Middleware in Laravel acts like a gatekeeper for incoming requests. What is the main reason Laravel uses middleware to filter requests?
ATo generate HTML views from controllers
BTo speed up database queries automatically
CTo check and modify requests before they reach the application logic
DTo store user data permanently on the server
Attempts:
2 left
💡 Hint
Think about how middleware can control access or change requests before the app handles them.
component_behavior
intermediate
2:00remaining
What happens if middleware denies a request?
In Laravel, if a middleware decides a request should not proceed, what happens next?
AThe request is logged but still processed normally
BThe request continues to the controller but with a warning
CThe middleware crashes the application
DThe request is stopped and a response is sent back immediately
Attempts:
2 left
💡 Hint
Middleware can block requests to protect the app.
state_output
advanced
2:00remaining
What is the output when middleware modifies a request header?
Consider a Laravel middleware that adds a custom header X-Custom-Header: Hello to every request. What will the controller receive?
Laravel
<?php
// Middleware handle method
public function handle($request, Closure $next) {
    $request->headers->set('X-Custom-Header', 'Hello');
    return $next($request);
}

// Controller method
public function index(Request $request) {
    return $request->header('X-Custom-Header');
}
A"Hello"
Bnull
C"x-custom-header"
DThrows an error
Attempts:
2 left
💡 Hint
Middleware can add or change headers before the controller sees the request.
📝 Syntax
advanced
2:00remaining
Identify the correct middleware syntax to allow only authenticated users
Which middleware code snippet correctly checks if a user is authenticated and redirects if not?
A
public function handle($request, Closure $next) {
    if (!auth()-&gt;check()) {
        return redirect('login');
    }
    return $next($request);
}
B
public function handle($request, Closure $next) {
    if (auth()-&gt;user()) {
        return redirect('login');
    }
    return $next($request);
}
C
public function handle($request, Closure $next) {
    if (auth()-&gt;guest()) {
        return redirect('login');
    }
    return $next($request);
}
D
public function handle($request, Closure $next) {
    if (auth()-&gt;check) {
        return redirect('login');
    }
    return $next($request);
}
Attempts:
2 left
💡 Hint
Check the correct method to verify authentication and syntax for method calls.
🔧 Debug
expert
3:00remaining
Why does this middleware not stop unauthenticated users?
Given this middleware code, why does it fail to block unauthenticated users?
Laravel
public function handle($request, Closure $next) {
    if (auth()->check()) {
        return redirect('login');
    }
    return $next($request);
}
AIt uses the wrong method to check authentication
BIt redirects authenticated users instead of unauthenticated ones
CIt does not return the next middleware
DIt throws a syntax error because of missing semicolons
Attempts:
2 left
💡 Hint
Look carefully at the condition and who it redirects.