LoginSignup
0
1

More than 3 years have passed since last update.

【Laravel】名前付きルートを設定して、ルート定義をもっと便利にしよう

Last updated at Posted at 2020-07-31

Laravelのルートインスタンスには、nameメソッドという「定義したルートに名前をつける」便利なものがあります。
ルートインスタンスに、このnameメソッドをチェーンするだけなので気軽に使用できます。

routes/web.php
Route::get('user/profile', function () {
    //
})->name('profile');

上記の例ではルートに「profile」という名前が付いています。
このように名前が付けられることによって、
特定のルートへのURLを作成できたり、
または、リダイレクト時にルート定義で付けた名前を指定することが可能になります。

もちろん、コールバックがクロージャではなく、コントローラアクションの場合でも名前付きルートが設定できます。

routes/web.php
Route::get('user/profile', 'UserProfileController@show')->name('profile');

こちらも、「profile」という名前が付きました
こんな感じで、名前付きルートを設定してみましょう。

0
1
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
1