Markdown

[symfony] The HttpFoundation Component

http://symfony.com/doc/current/components/http_foundation.html

Request


use Symfony\Component\HttpFoundation\Request;
1
2
3
4
5
6
7
8
$request = new Request(
    $_GET,
    $_POST,
    array(),
    $_COOKIE,
    $_FILES,
    $_SERVER
);
---

Accessing Request Data

A Request object holds information about the client request. This information can be accessed via several public properties:
  • request: equivalent of $_POST;
  • query: equivalent of $_GET ($request->query->get('name'));
  • cookies: equivalent of $_COOKIE;
  • attributes: no equivalent - used by your app to store other data (see below);
  • files: equivalent of $_FILES;
  • server: equivalent of $_SERVER;
  • headers: mostly equivalent to a subset of $_SERVER ($request->headers->get('User-Agent')).
Each property is a ParameterBag instance (or a sub-class of), which is a data holder class:
All ParameterBag instances have methods to retrieve and update their data:
all()
Returns the parameters.
keys()
Returns the parameter keys.
replace()
Replaces the current parameters by a new set.
add()
Adds parameters.
get()
Returns a parameter by name.
set()
Sets a parameter by name.
has()
Returns true if the parameter is defined.
remove()
Removes a parameter.
The ParameterBag instance also has some methods to filter the input values:
getAlpha()
Returns the alphabetic characters of the parameter value;
getAlnum()
Returns the alphabetic characters and digits of the parameter value;
getBoolean()
Returns the parameter value converted to boolean;
getDigits()
Returns the digits of the parameter value;
getInt()
Returns the parameter value converted to integer;
filter()
Filters the parameter by using the PHP filter_var function.

// the query string is '?foo=bar'

$request->query->get('foo');
// returns 'bar'

$request->query->get('bar');
// returns null

$request->query->get('bar', 'baz');
// returns 'baz'

---

Simulating a Request

Instead of creating a request based on the PHP globals, you can also simulate a request:
1
2
3
4
5
$request = Request::create(
    '/hello-world',
    'GET',
    array('name' => 'Fabien')
);
---

Creating a JSON Response

Any type of response can be created via the Response class by setting the right content and headers. A JSON response might look like this:
1
2
3
4
5
6
7
use Symfony\Component\HttpFoundation\Response;

$response = new Response();
$response->setContent(json_encode(array(
    'data' => 123,
)));
$response->headers->set('Content-Type', 'application/json');
There is also a helpful JsonResponse class, which can make this even easier:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use Symfony\Component\HttpFoundation\JsonResponse;

// if you know the data to send when creating the response
$response = new JsonResponse(array('data' => 123));

// if you don't know the data to send when creating the response
$response = new JsonResponse();
// ...
$response->setData(array('data' => 123));

// if the data to send is already encoded in JSON
$response = JsonResponse::fromJsonString('{ "data": 123 }');
---
The JsonResponse class sets the Content-Type header to application/json and encodes your data to JSON when needed.

留言