0
1

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のRoutingについて prefixとgroupを使ってみる

Last updated at Posted at 2020-06-08

laravelのルーティングについて簡単にまとめました。

基本の書き方

Route::get('/main/list','MainController@list')->name('main.list');
Route::get('/main/edit','MainController@edit')->name('main.edit');
Route::get('/main/search','MainController@search')->name('main.search');

prefixを使って省略した書き方

プレフィックスの意味は、頭に付く文字列のことです。
プレフィックス (prefix)とは|「分かりそう」で「分からない」でも「分かった」気になれるIT用語辞典

上記では/main/というプレフィックス部分が重複しているため、まとめて書くことで可読性の高いコードにします。prefixは通常、group関数とセットで使用されます。

Route::prefix('main')->group(function () {
    Route::get('list','MainController@list')->name('main.list');
    Route::get('edit','MainController@edit')->name('main.edit');
    Route::get('search','MainController@search')->name('main.search');
});

名前付きルート name

name('main.list');main.の部分も共通しているので、name()を使ってまとめて記載します。

Route::prefix('main')->name('main.')->group(function () {
    Route::get('list','MainController@list')->name('list');
    Route::get('edit','MainController@edit')->name('edit');
    Route::get('search','MainController@search')->name('search');
});
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?