symfony小部分文档翻译之http的介绍

由于个人兴趣、工作和学习的需要,我了解过smarty模板引擎;drupal、tp(3.2及之前版本)、Yii2.0、laravel等开源项目。最近我在drupal、Yii2.0、laravel中都发现了symfony的组件,于是去了解了一下symfony。我发现,作为一个成功的开源项目来说,文档真的很重要。因为symfony的文档详细到让我无法相信。下面就翻译一些基础的,这些基础部分对于新手来说是很有帮助的。(如果有翻译的不好的地方还请指正)

Symfony和HTTP基础工具(部分)

HTTP很简单

HTTP (Hypertext Transfer Protocol to the geeks) is a text language that allows two machines to communicate with each other. That’s it! For example, when checking for the latest xkcd comic, the following (approximate) conversation takes place:

HTTP是一个允许两台机器相互交流的文本语言。例如,当访问xkcd漫画网站的时候,会发生下面的交流:

symfony小部分文档翻译之http的介绍_第1张图片

And while the actual language used is a bit more formal, it’s still dead-simple. HTTP is the term used to describe this simple text-based language. No matter how you develop on the web, the goal of your server is always to understand simple text requests, and return simple text responses.

并且真正的使用中会更正式一些,上面画的是很简单的。HTTP是用来描述这种简单的基于文本的语言的术语。不管你怎么在web服务器上面做开发,你的服务器的最终目的总是去理解简单的文本请求,并且返回简单的文本回复。

第一步:客户端发出请求

Every conversation on the web starts with a request. The request is a text message created by a client (e.g. a browser, a smartphone app, etc) in a special format known as HTTP. The client sends that request to a server, and then waits for the response.

在服务器上,所有的交流都是开始于请求。请求是一个被客户端(比如浏览器,app)创建的固定格式的文本消息,我们知道这种固定格式的标准叫做HTTP。客户端发送请求到服务器上,然后等待回复。

Take a look at the first part of the interaction (the request) between a browser and the xkcd web server:

看看在浏览器和xkcd网站之间交流的第一步:

symfony小部分文档翻译之http的介绍_第2张图片

In HTTP-speak, this HTTP request would actually look something like this:

在HTTP交互中,这个HTTP请求实际上看起来会像这样:

GET / HTTP/1.1
Host: xkcd.com
Accept: text/html
User-Agent: Mozilla/5.0 (Macintosh)

This simple message communicates everything necessary about exactly which resource the client is requesting. The first line of an HTTP request is the most important and contains two things: the URI and the HTTP method.

这个简单的消息交代了所有关于客户端所请求的资源的必要的信息。第一行是很重要的,它包含两个意思:URI和HTTP方法(method)。

The URI (e.g. //contact, etc) is the unique address or location that identifies the resource the client wants. The HTTP method (e.g. GET) defines what you want to do with the resource. The HTTP methods are the verbs of the request and define the few common ways that you can act upon the resource:

URI(例如 /,/contact这样的)是定位一个资源的唯一地址。HTTP方法定义客户端想要对资源进行的操作。HTTP方法在请求中是动词形式的,并且定义了一些通用的对资源的操作:

GET 获取服务器中的资源
POST 在服务器上创建资源
PUT 更新服务器上的资源
DELETE 删除服务器上的资源

With this in mind, you can imagine what an HTTP request might look like to delete a specific blog entry, for example:

通过这些,你可以想象在删除一条特定的博客记录是HTTP请求会是个什么样子,比如:

DELETE /blog/15 HTTP/1.1

注解:

There are actually nice HTTP methods defined by the HTTP specification, but many of them are not widely used or supported. In reality, many modern browsers don’t even support the PUT andDELETE methods.

实际上HTTP是像上面一样被设计的很完美,但是很多的浏览器没有完全的支持它。在实际使用中,很多现代的浏览器并没有支持PUT和DELETE这两个方法。

In addition to the first line, an HTTP request invariably contains other lines of information called request headers. The headers can supply a wide range of information such as the requested Host, the response formats the client accepts (Accept) and the application the client is using to make the request (User-Agent). Many other headers exist and can be found on Wikipedia’s List of HTTP header fields article.

除了第一行之外,一个HTTP请求总是包含另一行被叫做HTTP头的信息。头可以包含大量的信息,比如,Host,客户端所接受的数据格式的说明Accept和客户端所使用的发送请求的程序User-Agent。很多其他的头可以去维基百科找。

第二步:服务端返回响应

Once a server has received the request, it knows exactly which resource the client needs (via the URI) and what the client wants to do with that resource (via the method). For example, in the case of a GET request, the server prepares the resource and returns it in an HTTP response. Consider the response from the xkcd web server:

当服务器收到请求后,它清楚的知道客户端需要哪个资源(通过URI知道的),并且知道客户端想要对资源做什么操作(通过方法知道的)。比如,在get请求的例子中,服务器找到资源并且在一个回复中返回资源。考虑xkcd网站服务器的回复:

symfony小部分文档翻译之http的介绍_第3张图片

Translated into HTTP, the response sent back to the browser will look something like this:

翻译成HTTP,回复看起来是这样的:

HTTP/1.1 200 OK
Date: Sat, 02 Apr 2011 21:05:05 GMT
Server: lighttpd/1.4.19
Content-Type: text/html


  

The HTTP response contains the requested resource (the HTML content in this case), as well as other information about the response. The first line is especially important and contains the HTTP response status code (200 in this case). The status code communicates the overall outcome of the request back to the client. Was the request successful? Was there an error? Different status codes exist that indicate success, an error, or that the client needs to do something (e.g. redirect to another page). A full list can be found on Wikipedia’s List of HTTP status codes article.

HTTP回复包含被请求的资源(在这个例子中是html代码),还有一些其他的关于回复的信息。第一行尤其重要,它包含HTTP状态码(在这个例子中是200)。状态码表示整体的返回状态:请求是否成功?有没有出问题?不同的状态码表示不同的状态。(。。。)状态码列表可以在维基百科上找到。

Like the request, an HTTP response contains additional pieces of information known as HTTP headers. For example, one important HTTP response header is Content-Type. The body of the same resource could be returned in multiple different formats like HTML, XML, or JSON and theContent-Type header uses Internet Media Types like text/html to tell the client which format is being returned. A list of common media types can be found on Wikipedia’s List of common media types article.

就像请求一样,一个HTTP回复包含附加的碎片信息叫做HTTP头。比如,一个重要的头叫做Content-Type。一个资源的表示方式可以是多重不同的形式,比如HTML、XML或者JSON。Content-Type使用像text/html这样的Internet Media Types来告诉客户端什么格式被返回。media types列表可以在维基百科上找到。

Many other headers exist, some of which are very powerful. For example, certain headers can be used to create a powerful caching system.

有很多其他的头存在,有些很强大。比如certain可以用来创建强大的缓存系统。

PHP里的请求和响应

So how do you interact with the “request” and create a “response” when using PHP? In reality, PHP abstracts you a bit from the whole process:

那么问题来了。你是怎么使用PHP与请求互动,并且怎么创建回复的?实际上,PHP帮你从整个流程中抽象出了一些东西:

$uri = $_SERVER['REQUEST_URI'];
$foo = $_GET['foo'];
header('Content-Type: text/html');
echo 'The URI requested is: '.$uri;
echo 'The value of the "foo" parameter is: '.$foo;


As strange as it sounds, this small application is in fact taking information from the HTTP request and using it to create an HTTP response. Instead of parsing the raw HTTP request message, PHP prepares superglobal variables such as $_SERVER and $_GET that contain all the information from the request. Similarly, instead of returning the HTTP-formatted text response, you can use the header()function to create response headers and simply print out the actual content that will be the content portion of the response message. PHP will create a true HTTP response and return it to the client:

可能你对这个比较陌生,PHP 实际上已经帮你获取到了HTTP请求的信息,并且帮你创建的HTTP的回复。PHP生成包含请求信息的超级全局变量,比如,$_SERVER和$_GET,而不需要你去处理HTTP请求消息。同样的,你可以使用header()函数来创建消息头并且简单的输出类容,而不是亲自生成HTTP格式的回复。PHP会创建一个HTTP回复并且返回给客户端:

HTTP/1.1 200 OK
Date: Sat, 03 Apr 2011 02:14:33 GMT
Server: Apache/2.2.17 (Unix)
Content-Type: text/html

The URI requested is: /testing?foo=symfony
The value of the "foo" parameter is: symfony


转载于:https://my.oschina.net/zedisdog/blog/619189

你可能感兴趣的:(symfony小部分文档翻译之http的介绍)