基于REST的API - 如何返回相同的资源不同的结果? [英] RESTful API - How do I return different results for the same resource?

查看:458
本文介绍了基于REST的API - 如何返回相同的资源不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我如何返回不同的结果为相同的资源?

详细信息

我一直在寻找,现在要建造一个RESTful API的正确方法一段时间。伟大的信息吨那里。现在,我实际上是试图将此应用到我的网站,并已遇到一些障碍。我发现了一些建议,说你的基础数据库作为起点上的资源,考虑到你的数据库应该体面结构。这里是我的情况:

我的网站:

下面是关于我的网站一点点信息和API的目的

我们正在创建一个网站,让人们玩游戏。该API应该允许其他开发人员构建自己的游戏,并使用我们的后台收集用户信息和存储。

方案1:

我们有一个球员存储所有的玩家数据数据库。开发者需要的基础上无论是user_ID的(人谁拥有的球员数据)或game_id(所收集的数据进行游戏)选择此数据。

资源

  http://site.com/api/players

问题:

如果开发者使用叫我的资源 GET 他们将获得的球员名单。由于有多个开发人员使用这个系统,他们必须指定一些ID由选择的所有球员。这是我发现的一个问题。我希望开发人员能够指定2种编号的。他们可以通过USER_ID或game_id选择所有的球员。


  1. 你怎么处理呢?

  2. 请我需要两个单独的资源?


解决方案

比方说你有一个控制器名称'玩家',那么你就会有2种方法:

 函数user_get(){
   //从请求获取ID和做一些事情
}功能game_get(){
//从请求获取ID和做一些事情
}

现在的URL看起来像: http://site.com/api/players/user / 333 http://site.com/api/players/game/333


  1. 玩家控制器。

  2. 用户/游戏是动作

如果您使用菲尔鲟鱼的框架,你会做,但是URL看起来像:
http://site.com/api/players/user/id/333 http://site.com/api/players/game/id/333

,然后你用得到ID: $这个 - >获取('身份证');

Question

How do I return different results for the same resource?

Details

I have been searching for some time now about the proper way to build a RESTful API. Tons of great information out there. Now I am actually trying to apply this to my website and have run into a few snags. I found a few suggestions that said to base the resources on your database as a starting point, considering your database should be structured decently. Here is my scenario:

My Site:

Here is a little information about my website and the purpose of the API

We are creating a site that allows people to play games. The API is supposed to allow other developers to build their own games and use our backend to collect user information and store it.

Scenario 1:

We have a players database that stores all player data. A developer needs to select this data based on either a user_id (person who owns the player data) or a game_id (the game that collected the data).

Resource

http://site.com/api/players

Issue:

If the developer calls my resource using GET they will receive a list of players. Since there are multiple developers using this system they must specify some ID by which to select all the players. This is where I find a problem. I want the developer to be able to specify two kinds of ID's. They can select all players by user_id or by game_id.

  1. How do you handle this?
  2. Do I need two separate resources?

解决方案

Lets say you have a controller name 'Players', then you'll have 2 methods:

function user_get(){
   //get id from request and do something
}

function game_get(){
//get id from request and do something
}

now the url will look like: http://site.com/api/players/user/333, http://site.com/api/players/game/333

  1. player is the controller.
  2. user/game are the action

If you use phil sturgeon's framework, you'll do that but the url will look like: http://site.com/api/players/user/id/333, http://site.com/api/players/game/id/333

and then you get the id using : $this->get('id');

这篇关于基于REST的API - 如何返回相同的资源不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆