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】「web.php のルート情報が多くなった…」ときに便利な ルート情報のファイル分割

Posted at

はじめに

Laravel で routes/web.php にルーティング情報を記述していくと、いつの間にか web.php 内が多くのルート情報で埋め尽くされてしまうことがあります。

そんなときは、ルート情報を 別のファイルに分割する 方法をとると、web.php がすっきりして、尚且つ、目的に応じたルート情報ファイルを作成することで、メンテナンス向上にも繋がるかと思います。

目的別にルート情報を分割する

目的別に情報を分割する方法として、web.phprequire を使用して、ルート情報が記述されたファイルを取り込む方法があります。以下の様な感じです。

require __DIR__ . '/sample.php';

例えば、Laravel Breeze をインストールした後は、web.php が以下の様になっています。

web.php
Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

require __DIR__ . '/auth.php';

require __DIR__ . '/auth.php'; の部分を見て分かるように、認証関連のルート情報は、routes/auth.php のファイルを取り込んで処理しています。

この様に、ルート情報を目的別に分けてファイル管理することによって、web.php 内が見やすくなり、メンテナンス性も向上します。

0
0
0

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?