0

在使用 Routes 时,我注意到 Laravel 4 中的一些特殊之处。我有一个如下所示的路由组:

// 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结果找不到路由?

4

1 回答 1

1

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

{id}是一条“包罗万象”的路线。

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

因此,将您的“包罗万象”路线放在列表的底部 - 它会正常工作。

于 2013-08-25T11:25:25.623 回答