简体   繁体   English

RESTful嵌套Laravel路由

[英]RESTful nested Laravel routes

I'm attempting to create an API URL structure with nested Laravel routes like the following: 我正在尝试使用嵌套的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

I tried doing this with nested resources and two controllers. 我尝试使用嵌套资源和两个控制器来执行此操作。 The TableController (tied to a Table model) would control the data in the table, and the TableSettings (tied to a TableSettings model) controller would control the settings (column names, order, visibility, etc). TableController (绑定到Table模型)将控制表中的数据,而TableSettings (绑定到TableSettings模型)将控制设置(列名,顺序,可见性等)。 The idea being that you would call /api/v1/tables/<id> to get the data for the table and /api/v1/tables/<id>/settings to get the settings, then use this to build the display. 这样的想法是,您将调用/api/v1/tables/<id>来获取表的数据,并调用/api/v1/tables/<id> /api/v1/tables/<id>/settings来获取设置,然后使用它来构建显示。

In my routes.php I have: 在我的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')));
});

I'd like to do something to this effect with to keep routes.php as clean as possible. 我想为此做些事情,以保持routes.php尽可能干净。 The problem I'm running into is that when I try to hit either the settings edit or update URL ( /api/v1/tables/<id>/settings/<edit|update> ) it's actually looking for a URL in the form of /api/v1/tables/<id>/settings/<another_id>/edit . 我遇到的问题是,当我尝试点击设置编辑或更新URL( /api/v1/tables/<id>/settings/<edit|update> )时,它实际上是在查找表单中的URL /api/v1/tables/<id>/settings/<another_id>/edit But I want it to use the table's ID rather than having a whole new settings ID in the URL. 但我希望它使用表的ID,而不要在URL中使用全新的设置ID。

Is there a way to use a nested resource controller in this way? 有没有办法以这种方式使用嵌套资源控制器? Or should I use another method? 还是应该使用其他方法?

If you re-arrange the order of the resources - I think that will work: 如果您重新安排资源的顺序-我认为这会起作用:

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