LoginSignup
0
0

More than 1 year has passed since last update.

ルーティングの優先度・順番

Last updated at Posted at 2022-01-08

ルーティングには上から順番に処理がされていくという規則があります。

例えば下記のようなコードの場合、

web.php
Route::get('/picture/{id}', 'UserController@show')->name('shop.show');
Route::get('/picture/edit', 'UserController@edit')->name('shop.edit');

『~/picture/edit』のURIを表示しようとすると
先に記載されているRoute::get('/picture/{id}', 'UserController@show')->name('shop.show');
こちらの処理がされます。

URI『~/picture/edit』の画面に遷移した際、
{id}の中にeditが入ってしまいます。カラムにeditという値が存在しない為、エラーが表示されうまく画面遷移がされません。

解決策

パラメータのあるルーティングは下にする

web.php
Route::get('/picture/edit', 'UserController@edit')->name('shop.edit');
Route::get('/picture/{id}', 'UserController@show')->name('shop.show');

こうすることで、
URIが『~picture/edit』に画面遷移が可能となり、editメソッドの処理が可能となります。

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