0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】ルートグループを定義して、まとまり感のあるルートを作成する

Last updated at Posted at 2020-08-03

ルートグループは、ミドルウェアや名前空間のようなルート属性を、一括して複数のルートに適応させる手法です。Route::groupメソッドを使用します。

指定のミドルウェアをルートグループに適応させる

グループとしてまとめられた各ルートにミドルウェアを指定した例がこちら。

routes/web.php
Route::middleware(['first', 'second'])->group(function () {
    Route::get('/', function () {
        // firstとsecondミドルウェアを使用
    });

    Route::get('user/profile', function () {
        // firstとsecondミドルウェアを使用
    });
});

Route::middlewareメソッドを使用してfirstミドルウェアとsecondミドルウェアを適応させることが記述されています。このRoute::middlewaregroupメソッドをチェーンする形で書いていきましょう。グループ内の各ルートにRoute::middlewareメソッドで指定したミドルウェアが適応されます。

このグループ内の各ルートは、firstミドルウェア、secondミドルウェアの順に実行されます。実行される順はRoute::middlewareメソッドに指定した配列の順序となります。

参考文献:https://readouble.com/laravel/7.x/ja/routing.html

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?