Skip to main content

Drupal Plugin

Displaying 1 - 1 of 1

ResourceResponse When and How

class ResourceResponse
Contains data for serialization before sending the response.
We do not want to abuse the $content property on the Response class to store our response data. 
$content implies that the provided data must either be a string or an object with a __toString() method, which is not a requirement for data used here.
Routes that return this response must specify the '_format' requirement.

$response = ['message' => 'Hello, this is a rest service'];
return new ResourceResponse($response);
use Symfony\Component\HttpFoundation\Response;

$response = new Response(
    'Content',
    Response::HTTP_OK,
    ['content-type' => 'text/html']
);

$response->setContent('Hello World');

// the headers public attribute is a ResponseHeaderBag
$response->headers->set('Content-Type', 'text/plain');

$response->setStatusCode(Response::HTTP_NOT_FOUND);