use Symfony\Component\HttpFoundation\JsonResponse; $response = new JsonResponse([ 'message' => 'Hello, World!' ]); $response->send();
use Symfony\Component\HttpFoundation\RedirectResponse; $redirect = new RedirectResponse('/some-page.html'); $redirect->send();
use Symfony\Component\HttpFoundation\StreamedResponse; $response = new StreamedResponse(); $response->setCallback(function () { echo 'Hello, World!'; flush(); sleep(2); echo 'How are you?'; flush(); }); $response->send();In this last example, we use the StreamedResponse class to create a response whose content is generated dynamically and sent to the client in chunks. The setCallback method sets a function that generates the response on the fly. We then send the response to the client. Overall, the Response Factory package library provides a simple and flexible way to create HTTP responses in PHP. It is a valuable tool for any developer building web applications that need to generate and send HTTP responses.