How to Directly Pass The Middleware Instead of Register It in Laravel

Snippets 1 min read

If you have a middleware, and you want to use it in a specific route, you can pass it directly into the route without register it in the kernel file.

Route::get('posts', PostController::class)->middleware(['auth', CustomMiddleware::class]);

You can do the same on the controller also

class PostController extends Controller
{
	public function __construct()
	{
		$this->middleware(['auth', CustomMiddleware::class]);
	}
}

Happy Coding!


Related Tags

About the Author

Oussama's Profile Picture
Oussama
Full Stack Web Developer

I'm a full stack web developer and telecommunications engineer who love to share knowledge and build stuff online!


Comments

Join Our Newsletter

Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

Our news letter sent once a week, every tuesday.