2015-01-06 117 views
0

我试图创建一个嵌套的Laravel路由信息的API URL结构如下所示:REST风格的嵌套Laravel路线

/api/v1/tables -- list tables 
/api/v1/tables/<id> -- show one table data 
/api/v1/tables/<id>/update (post) -- update table data (inline editing of table, so edit screen not needed) 
/api/v1/tables/<id>/settings -- get settings for that table 
/api/v1/tables/<id>/settings/edit -- edit settings for that table 
/api/v1/tables/<id>/settings/update (post) -- save settings for that table 

我试着用嵌套资源,两个控制器这样做。 TableController(绑定到Table模型)将控制表中的数据,并且TableSettings(绑定到TableSettings模型)控制器将控制设置(列名,顺序,可视性等)。这个想法是,您将拨打/api/v1/tables/<id>来获取表格的数据并使/api/v1/tables/<id>/settings获得设置,然后使用它来构建显示。

在我routes.php我:

Route::group(array('prefix' => 'api/v1'), function() 
{ 
    Route::resource('tables', 'TablesController', 
        array('only' => array('index', 'show', 'update'))); 
    Route::resource('tables.settings', 'TableSettingsController'. 
        array('only' => array('index', 'edit', 'update'))); 
}); 

我希望做一些事情来这种效果来跟上routes.php尽可能干净。我遇到的问题是,当我尝试点击设置编辑或更新URL(/api/v1/tables/<id>/settings/<edit|update>)时,它实际上是以/api/v1/tables/<id>/settings/<another_id>/edit的形式查找URL。但我希望它使用表格的ID,而不是在URL中有全新的设置ID。

有没有办法以这种方式使用嵌套的资源控制器?或者我应该使用另一种方法?

回答

1

如果重新安排资源的顺序 - 我认为这将工作:

Route::group(array('prefix' => 'api/v1'), function() 
{ 
    Route::resource('tables.settings', 'TableSettingsController'. 
        array('only' => array('index', 'edit', 'update'))); 
    Route::resource('tables', 'TablesController', 
        array('only' => array('index', 'show', 'update'))); 
}); 
+0

不幸的是,没有运气与此有关。 '/ api/v1/tables/1/settings/edit'不起作用,但是'/ api/v1/tables/1/settings/1/edit'仍然有效。 – Samsquanch