繁体   English   中英

Laravel 4:组中的路由抛出notfoundhttpexception

[英]Laravel 4: route in group throwing notfoundhttpexception

我已经注意到在使用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结果?

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

{id}是“全部捕获”路由。

因此,路由系统看到/search并认为search是一个{id} -因此它将加载该路由。 但随后它找不到search ID-因此失败。

因此,将您的“全部捕获”路由保持在列表的底部-这样就可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM