我在组中获取路线时遇到问题。这是我的代码:
Route::group(['prefix' => 'commodities'], function(){
Route::get('commodities', [
'as' => 'showCommodities', 'uses' => 'CommodityController@showAll'
]);
Route::get('{id}', [
'as' => 'showCommodity', 'uses' => 'CommodityController@show'
]);
Route::get('add', [
'as' => 'addCommodity', 'uses' => 'CommodityController@create'
]);
Route::post('update', [
'as' => 'updateCommodity', 'uses' => 'CommodityController@update'
]);
Route::post('destroy', [
'as' => 'destroyCommodity', 'uses' => 'CommodityController@destroy'
]);
Route::post('add', [
'as' => 'storeCommodity', 'uses' => 'CommodityController@store'
]);
});
我在这里粘贴了CommodityController
代码http://pastebin.com/bWrdVhsv
除GET路线commodites/add
外,一切正常。我总是得到一个白页。我的调试设置为TRUE
,我有正确的刀片。
我在这里错过了什么吗?
答案 0 :(得分:5)
问题在于路线的顺序。
将add
路线移至所有{id}
路线上方。
Route::group(['prefix' => 'commodities'], function(){
Route::get('commodities', [
'as' => 'showCommodities', 'uses' => 'CommodityController@showAll'
]);
Route::get('add', [
'as' => 'addCommodity', 'uses' => 'CommodityController@create'
]);
Route::get('{id}', [
'as' => 'showCommodity', 'uses' => 'CommodityController@show'
]);
Route::post('update', [
'as' => 'updateCommodity', 'uses' => 'CommodityController@update'
]);
Route::post('destroy', [
'as' => 'destroyCommodity', 'uses' => 'CommodityController@destroy'
]);
Route::post('add', [
'as' => 'storeCommodity', 'uses' => 'CommodityController@store'
]);
});
Laravel将从头到尾浏览您的routes.php文件。以下路线基本上是一个全部捕获。
Route::get('{id}', [
'as' => 'showCommodity', 'uses' => 'CommodityController@show'
]);
这意味着它将捕获与模式匹配的网址的所有GET请求:
/commodities/some-kind-of-string
。
当/commodities/add
uri与上述模式匹配时,它将使用该路由,因为它首先出现在路径文件中。