laravel中嵌套资源路由的问题

时间:2014-04-22 18:54:38

标签: php laravel routing

我正在使用您可以找到here的初始网站,我正在尝试使嵌套资源路由正常工作。我有包含许多版本的项目,项目和版本URL都需要通常的查看/编辑/删除功能。我已经设置了这样的路线:

    # Project Management
    Route::get('projects/{project}', 'AdminProjectsController@getShow');
    Route::get('projects/{project}/edit', 'AdminProjectsController@getEdit');
    Route::post('projects/{project}/edit', 'AdminProjectsController@postEdit');
    Route::get('projects/{project}/delete', 'AdminProjectsController@getDelete');
    Route::post('projects/{project}/delete', 'AdminProjectsController@postDelete');
    Route::controller('projects', 'AdminProjectsController');

    # Version Management
    Route::get('projects/{project}/versions/{version}', 'AdminVersionsController@getShow');
    Route::get('projects/{project}/versions/{version}/edit', 'AdminVersionsController@getEdit');
    Route::post('projects/{project}/versions/{version}/edit', 'AdminVersionsController@postEdit');
    Route::get('projects/{project}/versions/{version}/delete', 'AdminVersionsController@getDelete');
    Route::post('projects/{project}/versions/{version}/delete', 'AdminVersionsController@postDelete');
    Route::controller('projects.versions', 'AdminVersionsController');

我遇到的问题是,当我访问/projects/1/versions/1这样的网址时,我收到此错误:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.

并且AdminVersionsController的getShow()方法确实存在,如下所示:

class AdminVersionsController extends AdminController {
    ....
    public function getShow($project, $version) {
        ....
    }
    ....
}

对于与版本相关的每个其他URL,我也会收到相同的错误。关于我做错了什么想法?

1 个答案:

答案 0 :(得分:0)

你的网址被这个宁静的路由集(称为_missing)所捕获:

Route::controller('projects', 'AdminProjectsController');

// the route is this:
GET|HEAD|POST|PUT|PATCH|DELETE projects/{_missing}

以及您对嵌套资源的要求是:

Route::resource('projects.versions', 'AdminProjectsController');

然后在该控制器中,您需要重命名方法以匹配资源控制器的方法:http://laravel.com/docs/controllers#resource-controllers