Laravel 4:组中投掷notfoundhttpexception的路线

时间:2013-08-25 09:33:24

标签: php laravel laravel-4

在使用路线时,我注意到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的结果?

1 个答案:

答案 0 :(得分:1)

这是预期的行为。路线以自上而下的方式进行评估。

{id}是一个“全部捕获”的路线。

因此,路线系统会看到/search - 并认为search{id} - 所以它会加载该路线。但是它找不到search的id - 所以它失败了。

因此,请在列表底部保留“全部捕获”路线 - 它将正常工作。