ホーム > Laravel > Writing routing with multiple middleware
Laravel

Writing routing with multiple middleware

Thank you for your continued support.
This article contains advertisements that help fund our operations.

Building on a previous article, I'll write about how to group multiple middlewares in routing.

⇨ Previously written, basic Laravel routing

⇨ Previously written, how to use authenticated user-only routes

⇨ Laravel documentation

Environment

Laravel 6

Use case

There are cases where you want only logged-in users + certain conditions to be able to use specific routes.

A classic example would be routes accessible only to "logged-in" + "email-verified" users.

How to group multiple middleware in routing

web.php

Route::group(['middleware' => ['auth','verified']], function () {
  // Write your routing code from previous articles inside this block
  // For example
  Route::get('/user', 'UserController@index');
  Route::get('/user/{id}', 'UserController@show');
});

Explanation

I have shown the syntax for specifying two middlewares, 'auth' and 'verified', simultaneously.

//before
['middleware' => 'auth']
//after
['middleware' => ['auth','verified']]

Since I simply turned the right side into an array, it should be easy to remember and jot down somewhere for future reference.

Conclusion

In this article, as a supplement to past articles, I wrote about how to specify multiple middlewares in routing.

If there are any confusing parts, errors, or strange statements, please feel free to let me know via DM on Twitter.

That's all for now.

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!