Requests and Responses in 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:1 2 3 4 5 6 | $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;
|
$_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:1 2 3 4 5 6 7 | 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
|
Requests and Responses in Symfony¶
Symfony provides an alternative to the raw PHP approach via two classes that allow you to interact with the HTTP request and response in an easier way. The
Request
class is a simple
object-oriented representation of the HTTP request message. With it, you
have all the request information at your fingertips:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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
As a bonus, the Request class does a lot of work in the background that
you'll never need to worry about. For example, the isSecure() method
checks the three different values in PHP that can indicate whether or not
the user is connecting via a secured connection (i.e. HTTPS).As seen above, the $_GET and $_POST variables are accessible via
the public query and request properties respectively. Each of
these objects is a ParameterBag
object, which has methods like
get() ,
has() ,
all() and more.
In fact, every public property used in the previous example is some instance
of the ParameterBag.
The Request class also has a public
attributes property, which holds
special data related to how the application works internally. For the
Symfony2 framework, the attributes holds the values returned by the
matched route, like _controller , id (if you have an {id}
wildcard), and even the name of the matched route (_route ). The
attributes property exists entirely to be a place where you can
prepare and store context-specific information about the request. |
No comments:
Post a Comment