REST-您能做得比拼写还要多吗? 第1部分

Thousands of years ago when we first started building web pages, things were very simple. You’d put some text on the page, maybe even an image, and that was pretty much it. But today it’s a whole different ball game. Instead of static pages there’s the dynamic applications we’ve come to depend on. And so, how these applications are designed to communicate becomes very important. In this series I’ll introduce you to the REST architecture style. In this article I’ll help you to understand exactly what it is, and later I’ll show you how it can be implemented in a PHP environment.

几千年前,当我们第一次开始构建网页时,事情非常简单。 您已经在页面上放置了一些文本,甚至是图像,仅此而已。 但是今天,这是一场完全不同的球赛。 除了静态页面之外,还有一些我们依赖的动态应用程序。 因此,如何设计这些应用程序进行通信变得非常重要。 在本系列中,我将向您介绍REST体系结构样式。 在本文中,我将帮助您确切地了解它的含义,稍后,我将向您展示如何在PHP环境中实现它。

什么是REST? (What is REST?)

REST is a set of principles that define how a server and client can communicate with each other (and external resources) in a simple, straightforward, and robust manner. Though you’ll often see the words “REST” and “architecture” together, REST is not a specific architecture. I like the words used by Dr. M. Elkstein in Learn REST: A Tutorial when he called it an “architecture style.” REST provides definite guidelines on how things should be built but does not insist things be built a certain way using certain building blocks.

REST是一组原则,用于定义服务器和客户端如何以简单,直接和可靠的方式相互通信(和外部资源)。 尽管您经常会同时看到“ REST”和“ architecture”两个词,但是REST不是特定的体系结构。 我喜欢M. Elkstein博士在“ 学习REST:教程”中使用的词,他将其称为“建筑风格”。 REST提供了有关如何构建事物的明确指导,但并未坚持使用某些构建块以某种方式构建事物。

In many references you will see REST and SOAP both mentioned as competitors. That is not true. SOAP is actually a protocol, not an architecture style. What REST can be compared against is SOA and RPC. All three are examples of web service styles, each with their own conceptual focus. RPC is focused around operations, SOA around messages, and REST around resources.

在许多参考资料中,您将看到REST和SOAP都被称为竞争对手。 那是不对的。 SOAP实际上是协议,而不是体系结构样式。 可以与REST进行比较的是SOA和RPC。 这三个都是Web服务样式的示例,每种样式都有自己的概念重点。 RPC专注于操作,SOA围绕消息,而REST则围绕资源。

The ideas behind REST have been around for a while, first appearing in the doctoral dissertation of Roy Fielding, one of the developers of the HTTP protocol. Indeed, the influence of REST on HTTP, and thus on the Internet itself, is clearly visible. But REST isn’t a standard or a protocol; there are no W3C documents to outline what it should be, and there are no version numbers to date it. If you do REST, you will certainly end up doing things a certain way, but that is not the same as saying it is a standard.

REST背后的思想已经存在了一段时间,最早出现在HTTP协议的开发者之一Roy Fielding的博士论文中。 确实,REST对HTTP以及对Internet本身的影响是显而易见的。 但是REST不是标准或协议。 没有W3C文档概述其应有的内容,并且至今没有版本号。 如果您使用REST,则肯定会以某种方式完成操作,但这与说它是标准不同。

REST stands for REpresentational State Transfer, and while this meant nothing to me the first time I saw it, Fielding had very definite ideas about its meaning.

REST代表REpresentational State Transfer ,虽然这对我第一次看到它并不意味着什么,但是Fielding对它的含义有非常明确的想法。

  • Representational refers to the fact that when you access something via the web what is returned to you is a representation of the object, not the actual object itself. If you go out and get a list of addresses, you could get back an XML data stream, not the actual database that stores the addresses.

    代表性表示是指这样的事实,当您通过网络访问某些内容时,返回给您的是对象的表示,而不是实际对象本身。 如果您出去获取地址列表,则可以获取XML数据流,而不是存储地址的实际数据库。
  • State refers to the belief that no session state should be kept on the server. Each request from the client should contain all of the necessary information for the server to understand the context of a request without referencing a previous transaction. All state is maintained by the client.

    状态是指不应在服务器上保留任何会话状态的信念。 来自客户端的每个请求应包含所有必要的信息,以便服务器理解请求的上下文而无需引用先前的事务。 所有状态均由客户端维护。
  • And Transfer refers to how data is shared across a network between clients and servers.

    传输是指如何在客户端和服务器之间通过网络共享数据。

REST原理 (Principles of REST)

So if REST is a set of principles, what are those principles?

因此,如果REST是一组原则,那么这些原则是什么?

Identify Everything as a URI – Everything in REST is a representation of some resource which can be identified by a unique URI (uniform resource identifier). This is the way the web itself is designed and it ensures a simplicity that is not available if resources are given a variety of different nomenclatures.

将所有内容标识为URI – REST中的所有内容都是某些资源的表示,可以通过唯一的URI(统一资源标识符)来标识。 这是Web本身的设计方式,它确保了简单性,如果为资源指定了各种不同的术语,这种简单性将不可用。

Hypermedia as the Engine of Application State – One of the beauties of the web is the ability to link from one page to another, so why not use that as the basis for all state transfer? Clients transition through a web application only through actions within the resource, such as hyperlinks.

超媒体作为应用程序状态的引擎 – Web的优点之一是能够从一页链接到另一页,那么为什么不将其用作所有状态转移的基础呢? 客户端只能通过资源内的操作(​​例如超链接)来通过Web应用程序进行转换。

Requests are Stateless – Central to REST is the idea of statelessness. That is, each request handled by the server can be done without knowing anything about any previous request. Or to put it another way, the client supplies all necessary information in the request needed by the server to fulfill it.

请求是无状态的 – REST的核心是无状态的概念。 也就是说,可以在不了解任何先前请求的情况下完成服务器处理的每个请求。 或者换一种说法,客户端在服务器实现它所需要的请求中提供所有必要的信息。

Standard Protocol Usage – there is nothing in REST that requires it to use HTTP. And yet, most of the time when you see a REST system it is built on HTTP. The reason for that is that HTTP uses four basic operations (GET, PUT, POST, and DELETE) to do all operations, and REST gravitates towards a small, standardized set of operations.

标准协议用法 – REST中没有要求它使用HTTP的内容。 但是,大多数时候,当您看到REST系统时,它是基于HTTP构建的。 这样做的原因是HTTP使用四个基本操作( GETPUTPOSTDELETE )来执行所有操作,而REST倾向于使用一组小的标准化操作。

In general, REST encourages simplicity and standardization, attempting to let the already existing protocol features do much of the heavy lifting and eliminating the need to re-invent these functions with programmer written operations. Although much of the web application development done today takes an RPC or SOA approach, there is a growing interest in using the more simple and yet robust style that REST advocates.

通常,REST鼓励简化和标准化,试图让已经存在的协议功能承担很多繁重的工作,并消除了通过程序员编写的操作重新发明这些功能的需求。 尽管当今完成的许多Web应用程序开发都采用RPC或SOA方法,但是对于使用REST倡导的更为简单而健壮的样式的兴趣却越来越浓厚。

PUT与POST (PUT vs. POST)

As I’ve noted above, REST is not a true architecture and it doesn’t force you to use certain tools or protocols. But most implementation use HTTP, primarily because of it’s simplicity. Every protocol has operations defined in it that perform the CRUD (Create, Retrieve, Update, and Delete) work required when data is involved. HTTP uses the four basic operations GET, PUT, POST, and DELETE to do it’s CRUD work.

如前所述,REST不是真正的架构,它不会强迫您使用某些工具或协议。 但是大多数实现使用HTTP,主要是因为它很简单。 每个协议中都有定义的操作,这些操作执行涉及数据时所需的CRUD(创建,检索,更新和删除)工作。 HTTP使用GETPUTPOSTDELETE这四个基本操作来完成CRUD工作。

As you might expect, GET retrieves data for you, and DELETE deletes data. And that brings us to PUT and POST. Discussing either of these, but especially POST is a little like discussing religion or politics; you are bound to upset someone. But here goes…

如您所料, GET为您检索数据,而DELETE删除数据。 这使我们进入PUTPOST 。 讨论其中任何一个,尤其是讨论POST都有点像讨论宗教或政治。 你注定会使某人不高兴。 但是这里...

Some people tend to relate the PUT to a create and the POST to an update. But I honestly think that is an over simplification, sort of like saying that the Dark Side of the Force is just what you experience on a day when you are a bit grouchy. In reality there are significant differences between the two methods.

有些人倾向于将PUT与创建相关联,将POST与更新相关联。 但老实说,我认为这过于简单了,就像说力量的阴暗面恰好是您在一天有些疲倦时所经历的。 实际上,这两种方法之间存在显着差异。

First, let’s start with the term “idempotent.” This is not to be confused with a similarly named, very serious medical condition for men which can only be treated by a number of drugs, none of which are street legal outside of Eastern Europe. Idempotent means that if you apply the operation multiple times, you will get the same result as if you had just applied it once. What’s the difference? It’s the difference between buying one authentic Dolly Parton figurines or five if you are shopping online and hit the Enter key a few times too often. As it turns out, GET, PUT, and DELETE are all idempotent. POST is not.

首先,让我们从术语“幂等”开始。 请勿将其与类似命名的非常严重的男性医疗状况相混淆,男性只能通过多种药物治疗,而这些药物在东欧以外都不是合法的。 幂等意味着,如果您多次应用该操作,您将获得与刚刚应用一次相同的结果。 有什么不同? 如果要在网上购物并多次按Enter键,则要购买一个正宗的Dolly Parton雕像或五个。 事实证明, GETPUTDELETE都是幂等的。 POST不是。

The only way you can do the same operation multiple times and not have it screw everything up is to transmit all of the data associated with the resource when you request the operation. Hence, that’s what PUT does. The server receives all the data available and completely rewrites the resource. It doesn’t matter whether the resource is already there or not; you are completely replacing it so create and update is treated the same.

可以多次执行相同操作且不让所有操作费解的唯一方法是在您请求操作时传输与资源关联的所有数据。 因此, PUT这样做的。 服务器接收所有可用数据,并完全重写资源。 资源是否已经存在都没有关系。 您将完全替换它,因此创建和更新被视为相同。

POST, on the other hand, is not idempotent. If you do it multiple times, multiple things will happen. For example, if you are creating a new user sign on and you use a POST, you are going to create a new account every time that you hit Enter. POST requests only require some, but not all, of the data associated with the resource. In fact it’s a very good if the data you are sending represents an addition to a resource that already exists, such as adding a comment to a blog post or an update to a trouble ticket. You follow me, slick?

另一方面, POST不是幂等的。 如果您多次执行此操作,则会发生多种情况。 例如,如果要创建一个新用户登录并使用POST ,则每次按Enter时都将创建一个新帐户。 POST请求仅需要与资源关联的部分数据,而不是全部。 实际上,如果您要发送的数据表示对已存在资源的添加是非常好的,例如在博客文章中添加评论或对故障单进行更新。 你跟着我,光滑吗?

Second, since REST is a resource-oriented architecture style (opposed to message-oriented as SOA is and operation-oriented as RPC), let’s look at this in terms of what is happening to the resource. PUT, an idempotent operation, affects the URI specified in the request. That is, it updates the resource at the URI that is provided by the client. POST affects a URI that is created and named by the server. The resource is updated with the data passed from the client, but the resource is separate and distinct from the client-named URI.

其次,由于REST是一种面向资源的体系结构样式(与SOA和RPC一样,都面向消息,而针对RPC是面向操作),因此让我们从资源方面来进行研究。 PUT是幂等操作,会影响请求中指定的URI。 即,它更新客户端提供的URI处的资源。 POST影响服务器创建和命名的URI。 使用从客户端传递的数据来更新资源,但是资源与客户端命名的URI是分开的且不同的。

A good question to ask when you are arguing with yourself about PUT vs POST is who should be naming the URI, the client side or the server? Another way to put it is whether you want to retain control over the URI or farm it out let the server create related URI’s. A blog post could be created with POST example.com/blog. The server would create the entry with ID 42, which would then be available as example.com/blog/42. Alternatively, a blog post could be created with PUT example.com/blog/42 if the resource didn’t already exist (in which case it would be replaced). The naming of the resource example.com/blog/42 was left up to the server with POST, and up to the client with PUT.

在与自己争论有关PUTPOST时,有一个很好的问题要问:谁应该命名URI,客户端还是服务器? 放置它的另一种方法是您是要保留对URI的控制权还是将其替换掉,让服务器创建相关的URI。 可以使用POST example.com/blog创建博客文章。 服务器将创建ID为42的条目,然后该条目可以作为example.com/blog/42 。 或者,如果资源尚不存在,则可以使用PUT example.com/blog/42创建博客帖子(在这种情况下,它将被替换)。 资源example.com/blog/42的命名由POST分配给服务器,由PUT分配给客户端。

摘要 (Summary)

REST is not a set way of doing things, it’s a state of mind based on a couple of simple principles: treat everything as a resource identified by a URI, use hyperlinks to move from one resource to another, treat everything as stateless, and use a standard protocol with generic operations. The advantage of REST over SOA or RPC is simplicity and speed (because there is less overhead both in what you have to build and in its processing) while still allowing you to build a solid and robust system. Most REST applications use HTTP (because of it’s simplicity) and so it is important to understand the difference between the PUT and POST methods.

REST不是一种固定的工作方式,它是基于两个简单原则的一种心态:将所有内容视为URI标识的资源,使用超链接从一个资源移动到另一个资源,将所有内容视为无状态,然后使用具有通用操作的标准协议。 与SOA或RPC相比,REST的优势在于简单性和速度(因为您必须构建的内容和处理过程中的开销都更少),同时仍允许您构建可靠的系统。 大多数REST应用程序都使用HTTP(因为它很简单),因此了解PUTPOST方法之间的区别非常重要。

In the next spine-tingling chapter of this saga, our hero and his plucky sidekick will show you how PHP fits in with all this. How can PHP be used with REST, and in particular how it interfaces with HTTP. I don’t know about you, but I’m stoked! I can’t wait to learn more about this exciting technique.

本故事的下一个刺痛的章节中,我们的英雄和他勇敢的助手将向您展示PHP如何与所有这些融为一体。 PHP如何与REST一起使用,尤其是如何与HTTP接口。 我不认识你,但我很激动! 我迫不及待地想了解更多有关这项令人兴奋的技术的信息。

Image via littlesam / Shutterstock

图片来自littlesam / Shutterstock

翻译自: https://www.sitepoint.com/rest-can-you-do-more-than-spell-it-1/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值