0
0
Laravelframework~20 mins

Controller middleware in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Controller Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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";
    }
}
AAll methods in the controller require the user to be authenticated before running.
BOnly the index method requires authentication; other methods are public.
CMiddleware runs after the controller methods finish executing.
DMiddleware is ignored when applied in the constructor.
Attempts:
2 left
💡 Hint
Think about when middleware runs in the request lifecycle.
📝 Syntax
intermediate
2: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?
A
public function __construct() {
    $this->middleware('auth')->only(['edit', 'update']);
}
B
public function __construct() {
    $this->middleware('auth')->except(['edit', 'update']);
}
C
public function __construct() {
    $this->middleware('auth', ['only' => ['edit', 'update']]);
}
D
public function __construct() {
    $this->middleware(['auth'])->only('edit', 'update');
}
Attempts:
2 left
💡 Hint
Check the correct method chaining syntax for middleware restrictions.
🔧 Debug
advanced
2: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";
    }
}
ABecause the middleware name 'auth' is misspelled and ignored.
BBecause 'show' method has its own middleware overriding the constructor middleware.
CBecause middleware only runs on POST requests, and 'show' is a GET method.
DBecause the middleware is set to run on all methods except 'show', so 'show' is public.
Attempts:
2 left
💡 Hint
Look at the except method usage in middleware.
state_output
advanced
2: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';
    }
}
AThe user sees a blank page because middleware does not handle errors.
BThe user sees a 403 Forbidden error page and the controller method is not executed.
CThe user sees 'Welcome to dashboard' because middleware runs after the method.
DThe user sees a 500 Internal Server Error because middleware crashed.
Attempts:
2 left
💡 Hint
Middleware can stop the request before the controller runs.
🧠 Conceptual
expert
2: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?
AMiddleware run in reverse order: 'log' first, then 'auth'.
BMiddleware run in parallel, so order does not matter.
CMiddleware run in the order they are added: 'auth' first, then 'log'.
DOnly the last middleware added runs, earlier ones are ignored.
Attempts:
2 left
💡 Hint
Think about the middleware stack order in Laravel.