简体   繁体   English

PHP REST API逻辑

[英]PHP REST API Logic

I have read several tutorials to introduce myself to know more about the rest API recently. 我阅读了一些教程,以介绍自己,以便最近了解更多有关其余API的信息。 However, I have got some doubts here and there and hope someone can help me out with this. 但是,我到处都有疑问,希望有人可以帮助我。

Reading the Beginner's Guide to HTML and REST , which states: 阅读HTML和REST入门指南 ,其中指出:

"Resources are best thought of as nouns. For example, the following is not RESTful: “资源最好被视为名词。例如,以下内容不是RESTful的:

1 /clients/add 1 /客户/添加

This is because it uses a URL to describe an action. 这是因为它使用URL来描述动作。 This is a fairly fundamental point in distinguishing RESTful from non-RESTful systems." 这是区分RESTful和非RESTful系统的相当基本的一点。”

As such, I was wondering if for such cases where I have a user resource and to access it to do the usual insert/update/delete/retrieve 因此,我想知道是否在这种情况下,我拥有用户资源并可以对其进行访问以执行通常的插入/更新/删除/检索

would be as follow: 如下:

www.example.com/users [get] <-- to retrieve all records www.example.com/users [获取] <-检索所有记录
www.example.com/users/1 [get] <-- to retrieve record with id of 1 www.example.com/users/1 [get] <-检索ID为1的记录
www.example.com/users/1 [put] <-- to update record with id of 1 www.example.com/users/1 [put] <-更新ID为1的记录
www.example.com/user/1 [delete] <-- to delete record with id of 1 www.example.com/user/1 [delete] <-删除ID为1的记录
www.example.com/user [post] <-- to insert a new user record www.example.com/user [post] <-插入新的用户记录

This would have used up the 4 common verbs to make request. 这将用完4个常用动词来提出请求。

What if I were to require a function such as login or perhaps in general any other types of action commands? 如果我需要诸如登录之类的功能,或者可能一般而言需要任何其他类型的动作命令,该怎么办? How should the url be formed and how should the router redirect in such cases? 在这种情况下,网址应如何形成?路由器应如何重定向?

EDIT: 编辑:

After looking at the various comments and answers. 看完各种评论和答案之后。 My take away from them is that the final solution would be somewhere along "use rest principles whenever possible and use the query string method with functions whenever not." 我对它们的看法是,最终的解决方案将是“尽可能使用rest原则,不使用带有功能的查询字符串方法”。

However, I was thinking of a slight variant of the implementation (not a restful implementation anymore, but following similar concepts) and wondering if it could have work out this way. 但是,我在考虑实现的一个细微变化(不再是一个宁静的实现,而是遵循类似的概念),并想知道它是否可以通过这种方式解决。 Hope you guys can advice me on this. 希望你们能给我建议。

Using the same authenticate/login function I would require to implement, could it be something along this instead: 使用我需要实现的相同身份验证/登录功能,是否可以执行以下操作:

www.example.com/users [get] <-- to retrieve all records www.example.com/users [获取] <-检索所有记录
www.example.com/users/1 [get] <-- to retrieve record with id of 1 www.example.com/users/1 [get] <-检索ID为1的记录
www.example.com/users/1 [put] <-- to update record with id of 1 www.example.com/users/1 [put] <-更新ID为1的记录
www.example.com/user/1 [delete] <-- to delete record with id of 1 www.example.com/user/1 [delete] <-删除ID为1的记录
www.example.com/user [post] <-- to insert a new user record www.example.com/user [post] <-插入新的用户记录

as usual and if I were to require an action to be performed it will be as such: 像往常一样,如果我要执行某项操作,它将是这样的:

[controller]/[action] --- user/authenticate [post] --- to login [controller] / [action] ---用户/验证[post] ---进行登录
[controller]/[id]/[action] --- user/1/authenticate [put] --- to logout [controller] / [id] / [action] ---用户/ 1 /验证[put] ---以注销

Will this work? 这样行吗? Will there be any foreseen problems that I would face and are there similar implementations out there like this already? 我会遇到任何可预见的问题吗,已经有类似的实现了吗? Please kindly advice! 请指教!

REST is stateless so you need to put all the needed information into all queries. REST是无状态的,因此您需要将所有必需的信息放入所有查询中。 The idea is to work with the HTTP Verbs (GET, PUT, DELETE, POST - as you already descripted). 想法是使用HTTP动词(如您所描述的,GET,PUT,DELETE,POST)。

If you want an user authentification for your REST API, use something like HTTP Basic Auth, or your own Authentification. 如果要对REST API进行用户身份验证,请使用HTTP基本身份验证或您自己的身份验证之类的方法。 You have to send the Auth Information for every Request to the Server (stateless). 您必须将每个请求的身份验证信息发送到服务器(无状态)。

If you don't want an HTTP Basic Auth you can try some Token Authentification or any other auth. 如果您不希望使用HTTP基本认证,则可以尝试某些令牌认证或任何其他认证。

Edit: If you want an "Check Login" Resource, build your own. 编辑:如果您想要“检查登录”资源,建立自己的。 For Example GET /account/checklogin with http basic auth header informations. 例如,使用GET / account / checklogin和http基本身份验证标头信息。 The Result of this Request depends on your Authinformations. 该请求的结果取决于您的Authinformation。

There are some actions that are hard to model in a true RESTful way - but login, for instance, can be implemented using the following pseudo code: 有些动作很难用真正的RESTful方式建模-例如,登录可以使用以下伪代码来实现:

GET the user rights whose userID is x and password is y
if (user rights found)
  assign rights to current user
else
  do not assign rights to user

See this question for how to retrieve the user rights. 有关如何检索用户权限的信息,请参见此问题 The point in this question is that you usually need multiple ways of accessing your resources. 这个问题的重点是您通常需要多种方式来访问资源。 Some are based on IDs or well-know attributes, for instance: 有些是基于ID或众所周知的属性的,例如:

  • www.example.com/users/department [get] (get all users for a department) www.example.com/users/department [获取](获取部门的所有用户)
  • www.example.com/users/roleName [get] (get all users in a particular role) www.example.com/users/roleName [获取](获取具有特定角色的所有用户)
  • www.example.com/users/status/active [get] (get all users who are "active") www.example.com/users/status/active [获取](获取所有处于“活动”状态的用户)

However, some ways of accessing users - especially when you need to combine two or more filtering attributes - are easier to manage using query string parameters. 但是,某些访问用户的方式-尤其是当您需要组合两个或多个过滤属性时-使用查询字符串参数更易于管理。 For instance: 例如:

www.example.com/users?department=xxx&role=yyy&status=active [get] www.example.com/users?department=xxx&role=yyy&status=active [获取]

So, your REST API might expose a URL along the lines of: 因此,您的REST API可能会公开以下网址:

www.example.com/users?userName=xxxx&password=yyy [get] www.example.com/users?userName=xxxx&password=yyy [get]

This URL would match the username and password parameters against the user database, and return either a 404 (if they don't match a known user), or a document representing the user, with their access rights. 该URL将针对用户数据库匹配用户名和密码参数,并返回404(如果它们与已知用户不匹配)或代表该用户及其访问权限的文档。

Your client code then manages the current user's session - ie by setting the status to "logged in ", and associating the session with that user profile. 然后,您的客户端代码将管理当前用户的会话-即通过将状态设置为“已登录”,并将该会话与该用户个人资料相关联。

The key to making this work is assigning responsibility to the right layer - the API should not have to manage user sessions, that is the responsibility of the client application. 进行这项工作的关键是将责任分配给正确的层-API不必管理用户会话,这是客户端应用程序的责任。 There are cases where that doesn't work particularly well - not sure if yours is one, though. 在某些情况下,这种方法不能很好地发挥作用-尽管不确定您是否是其中之一。

If you really want to use a POST request, you can, of course, consider the "login" method the start of a session for that user. 如果您确实要使用POST请求,则当然可以考虑将“ login”方法作为该用户会话的开始。 You could, therefore, do something like this: 因此,您可以执行以下操作:

www.example.com/session [POST] with parameters userID and password. www.example.com/session [POST],带有参数userID和密码。

This would return a representation of the user profile and rights; 这将返回用户个人资料和权限的表示; it might also create documents accessible under the URLs 它可能还会创建可通过URL访问的文档

www.example.com/session/sessionID www.example.com/session/sessionID

www.example.com/session/user/ID/session www.example.com/session/user/ID/session

However, in general, it is a very dangerous idea to manage session state within the API - nearly always, you want the client session to be managed by the application interacting with the client, not by the API it talks to. 但是,总的来说,在API中管理会话状态是一个非常危险的想法-几乎总是希望您由与客户端进行交互的应用程序(而不是与之交互的API)来管理客户端会话。

What if I were to require a function such as login or perhaps in general any other types of action commands? 如果我需要诸如登录之类的功能,或者可能一般而言需要任何其他类型的动作命令,该怎么办? How should the url be formed and how should the router redirect in such cases? 在这种情况下,网址应如何形成?路由器应如何重定向?

It's not RESTful to have a login- action resource, but it is RESTful to provide a login-form resource: 拥有登录操作资源不是RESTful的,但是提供登录表单资源 RESTful的:

/login-form

The HTML-form you return in the response functions as code-on-demand ; 您在响应中返回的HTML表单按需编码 you are supplying a configured piece of software to help the user supply their login credentials. 您正在提供已配置的软件,以帮助用户提供其登录凭据。

There would be nothing wrong with identifying the resource as just /login - I added the form-part to make the example clear. 将资源标识为/login并没有错-我添加了表单部分以使示例清晰明了。

You should avoid redirects where auth is required because it breaks the interface for clients other than web-browsers; 您应该避免在需要auth的地方进行重定向,因为它会破坏除Web浏览器之外的客户端的接口。 instead you might either: provide a link to the login-form; 相反,您可以:提供指向登录表单的链接; or actually supply the login-form code in the response. 或实际在响应中提供登录表单代码。

If you want to manage authentication, I prefer the approach of creating auth-tokens; 如果要管理身份验证,我更喜欢创建身份验证令牌的方法。 in the case of Web-browsers I consider it acceptable to overload a single cookie for the purpose of helping the client supply the token with each request since they will have no other reasonable way to control the Auth header they send; 对于Web浏览器,我认为可以重载单个cookie,以帮助客户端为每个请求提供令牌,因为它们将没有其他合理的方法来控制发送的Auth标头。 obviously if you're writing your own client-application this is not a concern. 显然,如果您正在编写自己的客户端应用程序,则不必担心。

Answering your comments below , the purpose of the login form in an auth-token scenario is to create a new authentication token. 在下面回答您的评论 ,在auth-token方案中登录表单的目的是创建一个新的身份验证令牌。 So, thinking RESTfully, you model the users list of auth-tokens and POST a representation of the auth-token. 因此,以REST方式思考,您可以对用户的身份验证令牌列表进行建模,并发布身份验证令牌的表示形式。 This representation might contain the user's username and password. 此表示形式可能包含用户的用户名和密码。 You might let the user choose their own token, or you might choose it for them and return this in the response. 您可以让用户选择自己的令牌,也可以为他们选择令牌,然后在响应中返回。 There is no action -URI required, and setting any cookies happens following successful creation of the new auth-token. 不需要任何操作 -URI,并且在成功创建新的auth-token后设置任何cookie。

I recommend studying Amazon S3 REST API. 我建议学习Amazon S3 REST API。 It's slightly different than your requirement but its the best in-depth description of a potential REST authentication system I've seen set out: 它与您的要求略有不同,但对我所看到的潜在REST身份验证系统的最佳深入描述是:

http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAPI.html http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAPI.html

Your thoughts on managing users RESTfully are accurate. 您关于完全管理用户的想法是正确的。

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM