The Middleware Method You're Missing
37sReveals a lesser-known Laravel feature with a clear before/after code comparison, sparking curiosity and saving viewers time.
▶ Play Clip"Delivers a useful, focused tip without fluff, though the title overpromises slightly by implying deeper coverage."
This video offers a concise Laravel tip on assigning middleware to specific controller methods, comparing route-level middleware with controller-level alternatives for better code readability.
Laravel allows assigning middleware to specific methods in routes using the middleware method, e.g., for the show method with can view blog, and auth and verified for other methods.
The presenter finds route-level middleware code hard to read, especially when multiple middleware are involved, and suggests more readable examples exist in Laravel docs.
For multiple middleware, the presenter recommends moving logic to gates and policies outside of routes for better organization.
Laravel allows controllers to implement HasMiddleware and provide an array of middleware for all or specific methods, as shown in the docs.
In Laravel 10 and older, middleware was assigned in the controller's constructor using the middleware method.
The video provides a quick, practical tip for Laravel developers to improve code readability by using controller-level middleware or gates/policies instead of cluttering routes with middleware logic.
How can you assign middleware to specific methods in Laravel routes?
Use the middleware method on the route, e.g., ->middleware('can:view-blog') for the show method.
What does the presenter recommend for handling multiple middleware in routes?
Move the logic to gates and policies outside of routes.
00:25
What interface must a controller implement to define middleware in Laravel 11+?
HasMiddleware.
00:37
How was middleware assigned in Laravel 10 and older?
In the controller's constructor using the middleware method.
00:49
Move logic to gates and policies
Provides a best-practice alternative to route-level middleware for better code organization.
00:25Controller-level middleware with HasMiddleware
Introduces a modern Laravel 11+ approach for cleaner middleware assignment.
00:37[00:00] Laravel tip how to assign a middleware to specific methods in controller. Did you know that there's a method called middleware4 that you can add in the routes? For the method show, we have middleware
[00:12] of can view blog. For a few more methods, we have auth and verified. But in my opinion, this code in the routes is pretty hard to read. Here are more readable examples in Laravel docs of middleware4
[00:25] for one method, I do agree, but if I have four middleware for lines, personally, instead, I would move the logic to gates and policies outside of routes. But also, you can put that logic in the
[00:37] controller. So, here's an example from the docs. You may specify that controller implements has middleware, and then provide the array of middlewares for all controller, or only for specific
[00:49] methods. In Laravel 10 and older, the syntax was this middleware in the constructor of controller. For more tips and longer videos, subscribe to my YouTube channel Laravel Daily.
⚡ Saved you 0h 01m reading this? Transcribe any YouTube video for free — no signup needed.