1.Symfony2 and HTTP Fundamentals

Step1: The Client sends a Request
一个HTTP请求如下所示:
GET / HTTP/1.1
Host: xkcd.com
Accept: text/html
User-Agent: Mozilla/5.0 (Macintosh)

第一行包含2个信息:URI和HTTP Method。
HTTP Method有如下几种:
GET     Retrieve the resource from the server
POST    Create a resource on the server
PUT     Update the resource on the server
DELETE  Delete the resource from the server

你可以使用http请求删掉一个指定的博客文章,例如:
DELETE /blog/15 HTTP/1.1 ; 但是需要注意的是,现在大多数的浏览器都不支持PUT和DELETE方法。

 
Step 2: The Server returns a Response 
当服务器收到一个请求后,它就可以从URI中知道client请求的是什么resource。

服务器返回给browser类似:
HTTP/1.1 200 OK
Date: Sat, 02 Apr 2011 21:05:05 GMT
Server: lighttpd/1.4.19
Content-Type: text/html

<html>
  <!-- ... HTML for the xkcd comic -->
</html>

如果想要了解更多的HTTP资料,可以参考http://www.w3.org/Protocols/rfc2616/rfc2616.html或者http://datatracker.ietf.org/wg/httpbis/ 。还有一个firefox插件专门Live HTTP Header专门用来查看header头的。


Requests and Responses in PHP
在PHP中,我们可以使用超全局变量得到http的数据,例如$_GET或$_SERVER['REQUEST_URI']等等。然后也可以代替浏览器输出自己需要的http信息,使用header头输出,例如:header('Content-type: text/html');


Requests and Responses in Symfony 
在Symfony中提供2个类来代替PHP原生的方法解决HTTP请求和返回的交互。
例如Request类如下:
use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

// the URI being requested (e.g. /about) minus any query parameters
$request->getPathInfo();

// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');

// retrieve SERVER variables
$request->server->get('HTTP_HOST');

// retrieves an instance of UploadedFile identified by foo
$request->files->get('foo');

// retrieve a COOKIE value
$request->cookies->get('PHPSESSID');

// retrieve an HTTP request header, with normalized, lowercase keys
$request->headers->get('host');
$request->headers->get('content_type');

$request->getMethod();          // GET, POST, PUT, DELETE, HEAD
$request->getLanguages();       // an array of languages the client accepts



Response类如下:
use Symfony\Component\HttpFoundation\Response;
$response = new Response();

$response->setContent('<html><body><h1>Hello world!</h1></body></html>');
$response->setStatusCode(200);
$response->headers->set('Content-Type', 'text/html');

// prints the HTTP headers followed by the content
$response->send(); 






A Symfony Request in Action 
假设你想要添加/contact页面到Symfony应用中,首先应该在routing配置文件中增加一个/contact入口,例如我们使用YAML配置:
# app/config/routing.yml
contact:
    path:     /contact
    defaults: { _controller: AcmeDemoBundle:Main:contact }

也可以使用PHP配置:
// app/config/routing.php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$collection = new RouteCollection();
$collection->add('contact', new Route('/contact', array(
    '_controller' => 'AcmeBlogBundle:Main:contact',
)));

return $collection;


Standalone Tools: The Symfony2 Components
下面是一些Symfony的独立组建,可以提出来单独使用:
  • HttpFoundation - Contains the Request and Response classes, as well as other classes for handling sessions and file uploads;
  • Routing - Powerful and fast routing system that allows you to map a specific URI (e.g./contact) to some information about how that request should be handled (e.g. execute thecontactAction() method);
  • Form - A full-featured and flexible framework for creating forms and handling form submissions;
  • Validator A system for creating rules about data and then validating whether or not user-submitted data follows those rules;
  • ClassLoader An autoloading library that allows PHP classes to be used without needing to manually require the files containing those classes;
  • Templating A toolkit for rendering templates, handling template inheritance (i.e. a template is decorated with a layout) and performing other common template tasks;
  • Security - A powerful library for handling all types of security inside an application;
  • Translation A framework for translating strings in your application.


你可能感兴趣的:(1.Symfony2 and HTTP Fundamentals)