2016-04-28 68 views
0

具有下一個路線:如何在路由時區分參數與聲明的路由?

Route::get('/apartment/{apartment_name}', '[email protected]'); 

Route::get('/apartment/create', [ 
      'uses' => '[email protected]', 
      'as' => 'apartment.create' 
     ]); 

Route::get('/apartment/edit', [ 
      'uses' => '[email protected]', 
      'as' => 'apartment.edit', 
     ]); 

我如何才能讓路由 myapp.com/apartment/createmyapp.com/apartment/beach-apartment之間的差異

我想通過公寓的名稱搜索相同的URI前綴(公寓/),但與此代碼我總是調用參數路由。

回答

1

這是因爲無論被調用的是創建還是編輯,正在匹配參數之一/apartment/{apartment_name},因爲createedit等於apartment_name

只需將參數1移動到該塊內最低的行。

Route::get('/apartment/create', [ 
      'uses' => '[email protected]', 
      'as' => 'apartment.create' 
     ]); 

Route::get('/apartment/edit', [ 
      'uses' => '[email protected]', 
      'as' => 'apartment.edit', 
     ]); 

Route::get('/apartment/{apartment_name}', '[email protected]'); 

根據該結構,如果/apartment/create/apartment/edit不匹配,那麼它將匹配/apartment/{apartment_name}