简体   繁体   English

Laravel 4:组中的路由抛出notfoundhttpexception

[英]Laravel 4: route in group throwing notfoundhttpexception

I've noticed a bit of a peculiarity in Laravel 4 when using Routes. 我已经注意到在使用Routes时Laravel 4有点特殊之处。 I have a Route group that looks like this: 我有一个路由组,如下所示:

// 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'));
));

I've noticed, however, that when I try and add in a new route I have to add it before the first route to use the {id} wildcard as the first parameter in it's url, otherwise I get a notfoundhttpexception . 但是,我注意到,当我尝试添加新路由时,必须在第一个路由之前添加它,以使用{id}通配符作为其url中的第一个参数,否则会得到notfoundhttpexception Is this normal? 这正常吗? So for example, this works (adding in the employers.search route: 因此,例如,这可行(将路径添加到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'));
}

Results in the route employers.search not being found? 找不到路线employers.search结果?

This is expected behavior. 这是预期的行为。 Routes are evaluated in a top-down fashion. 以自上而下的方式评估路线。

{id} is a "catch all" route. {id}是“全部捕获”路由。

So the route system sees /search - and thinks search is an {id} - so it loads that route. 因此,路由系统看到/search并认为search是一个{id} -因此它将加载该路由。 But then it cannot find an id of search - and so it fails. 但随后它找不到search ID-因此失败。

So keep your "catch all" route at the bottom of the list - and it will work correctly. 因此,将您的“全部捕获”路由保持在列表的底部-这样就可以正常工作。

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

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