Response : A Response object holds all the information that needs to be sent back to the client from a given request. The constructor takes up to three arguments: the response content, the status code, and an array of HTTP headers:
use Symfony\Component\HttpFoundation\Response;
$response = new Response(
'Content',
Response::HTTP_OK,
['content-type' => 'text/html']
);
This information can also be manipulated after the Response object creation :
$response->setContent('Hello World');
// the headers public attribute is a ResponseHeaderBag
$response->headers->set('Content-Type', 'text/plain');
$response->setStatusCode(Response::HTTP_NOT_FOUND);
When setting the Content-Type of the Response, you can set the charset, but it is better to set it via the setCharset()
method :
$response->setCharset('ISO-8859-1');
Note that by default, Symfony assumes that your Responses are encoded in UTF-8.
Sending the Response : Before sending the Response, you can optionally call the prepare() method to fix any incompatibility with the HTTP specification (e.g. a wrong Content-Type header):
$response->prepare($request);
Sending the response to the client is done by calling the method send()
: