【问题标题】:Laravel - nested routes without using a sub IDLaravel - 不使用子 ID 的嵌套路由
【发布时间】:2013-07-15 08:45:46
【问题描述】:

我正在 Laravel 中创建一个 REST API,但有一件事我想不通。情况如下;用户可以登录、浏览文章并将其收藏。

最喜欢的表由article_iduser_id组成

请求的设计方式如下:

  • [GET] /article/24 查看文章 24
  • [GET] /article/24/favorite 表示收藏,如果没有返回 404
  • [POST] /article/24/favorite 收藏
  • [DELETE] /article/24/favorite 删除收藏夹

看起来不错,但默认情况下嵌套资源需要以下格式:

/controller/[id]/sub_controller/[sub_id]

避免此sub id 要求的最佳方法是什么?我不需要收藏夹的 ID,因为 article iduser id 的组合足以在我的数据库中找到该项目。

这些是我当前的请求路线:

Route::resource('articles', 'ArticlesController', 
  array('only' => array('index','store','show','destroy')));

Route::resource('articles.favorite', 'ArticlesFavoriteController', 
  array('only' => array('show','store','destroy')));

目前,我要求 API 用户将 /0 附加到 url 以触发正确的路由,但必须有更好的方法。

【问题讨论】:

    标签: rest controller routes laravel


    【解决方案1】:

    我认为你应该考虑不要在这里使用 Laravel 的 ResourceController。也许您应该使用RestfullControllers 来满足您的特定需求。

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 2019-01-03
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2017-10-04
      • 2019-01-12
      相关资源
      最近更新 更多