簡體   English   中英

RESTful嵌套Laravel路由

[英]RESTful nested Laravel routes

我正在嘗試使用嵌套的Laravel路由創建API URL結構,如下所示:

/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> /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> )時,它實際上是在查找表單中的URL /api/v1/tables/<id>/settings/<another_id>/edit 但我希望它使用表的ID,而不要在URL中使用全新的設置ID。

有沒有辦法以這種方式使用嵌套資源控制器? 還是應該使用其他方法?

如果您重新安排資源的順序-我認為這會起作用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM