我注意到Laravel 4在使用Routes时有一点特殊性。我有一个路由组,看起来像这样:
// Employers routes
Route::group(array('prefix' => 'employers'), function(
Route::get('/', array('as' => 'employers.index', 'uses' => 'EmployersController@index'));
Route::get('create', array('as' => 'employers.create', 'uses' => 'EmployersController@create'));
Route::post('/', array('as' => 'employers.store', 'uses' => 'EmployersController@store', 'before' => 'csrf'));
Route::get('search', array('as' => 'employers.search', 'uses' => 'EmployersController@search'));
Route::get('{id}', array('as' => 'employers.show', 'uses' => 'EmployersController@show'));
Route::get('{id}/edit', array('as' => 'employers.edit', 'uses' => 'EmployersController@edit'));
Route::patch('{id}/update', array('as' => 'employers.update', 'uses' => 'EmployersController@update', 'before' => 'csrf'));
Route::delete('{id}/destroy', array('as' => 'employers.destroy', 'uses' => 'EmployersController@destroy', 'before' => 'csrf'));
));
然而,我注意到,当我尝试添加新路由时,我必须将其添加到第一个路由之前,才能使用{id}
通配符作为其url中的第一个参数,否则我会得到notfoundhttpexception
。这正常吗?例如,这是有效的(在employers.search
路由中添加:
// Employers routes
Route::group(array('prefix' => 'employers'), function(
Route::get('/', array('as' => 'employers.index', 'uses' => 'EmployersController@index'));
Route::get('create', array('as' => 'employers.create', 'uses' => 'EmployersController@create'));
Route::post('/', array('as' => 'employers.store', 'uses' => 'EmployersController@store', 'before' => 'csrf'));
Route::get('{id}', array('as' => 'employers.show', 'uses' => 'EmployersController@show'));
Route::get('search', array('as' => 'employers.search', 'uses' => 'EmployersController@search'));
}
是否导致未找到路由employers.search
?
这是预期行为。路线以自上而下的方式进行评估。
{id}
是一条"包罗万象"的路线。
所以路由系统看到/search
-并认为search
是{id}
-,所以它加载该路由。但随后它找不到search
的id,因此它失败了。
所以,把你的"一网打尽"路线放在列表的底部,它就会正常工作。