在使用路线时,我注意到Laravel 4有点特殊。我有一个如下所示的Route组:
// 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
的结果?
答案 0 :(得分:1)
这是预期的行为。路线以自上而下的方式进行评估。
{id}
是一个“全部捕获”的路线。
因此,路线系统会看到/search
- 并认为search
是{id}
- 所以它会加载该路线。但是它找不到search
的id - 所以它失败了。
因此,请在列表底部保留“全部捕获”路线 - 它将正常工作。