【问题标题】:Laravel 4: route in group throwing notfoundhttpexceptionLaravel 4:组中的路由抛出 notfoundhttpexception
【发布时间】:2013-08-25 09:33:23
【问题描述】:

在使用 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 中的第一个参数,否则我会得到 @987654323 @。这是正常的吗?因此,例如,这是可行的(在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 找不到?

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 感谢您的回复。我猜想这可能会按预期工作,但不太确定:)
    猜你喜欢
    • 2014-05-09
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2017-05-09
    • 2016-12-04
    • 2017-11-15
    • 2017-08-10
    • 2014-06-23
    相关资源
    最近更新 更多