-
-
Notifications
You must be signed in to change notification settings - Fork 0
ResponseFactory
Class ResponseFactory.
Factory for generating different types of HTTP responses. This class encapsulates a PSR-17 response factory and provides convenient methods for producing responses with content, payloads, redirects, or no content.
- Full name:
\FastForward\Http\Message\Factory\ResponseFactory - This class is marked as final and can't be subclassed
- This class implements:
\FastForward\Http\Message\Factory\ResponseFactoryInterface - This class is a Final class
Constructs the ResponseFactory.
public __construct(\Psr\Http\Message\ResponseFactoryInterface $responseFactory): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$responseFactory |
\Psr\Http\Message\ResponseFactoryInterface | the underlying PSR-17 response factory implementation |
Creates a standard HTTP response.
public createResponse(int $code = 200, string $reasonPhrase = ''): \Psr\Http\Message\ResponseInterfaceParameters:
| Parameter | Type | Description |
|---|---|---|
$code |
int | The HTTP status code. Defaults to 200 (OK). |
$reasonPhrase |
string | optional reason phrase |
Return Value:
the generated response
Creates an HTTP response containing HTML content.
public createResponseFromHtml(string $html): \Psr\Http\Message\ResponseInterfaceThe response SHALL have 'Content-Type: text/html' set automatically.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$html |
string | the HTML content to include in the response body |
Return Value:
the generated HTML response
Creates an HTTP response containing plain text content.
public createResponseFromText(string $text): \Psr\Http\Message\ResponseInterfaceThe response SHALL have 'Content-Type: text/plain' set automatically.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$text |
string | the plain text content to include in the response body |
Return Value:
the generated plain text response
Creates an HTTP 204 No Content response.
public createResponseNoContent(array $headers = []): \Psr\Http\Message\ResponseInterfaceThis response SHALL contain no body and have status code 204.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$headers |
array |
Return Value:
the generated no content response
Creates an HTTP response containing a JSON-encoded payload.
public createResponseFromPayload(array $payload): \FastForward\Http\Message\PayloadResponseInterfaceThe response SHALL have 'Content-Type: application/json' set automatically.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$payload |
array | the payload to encode as JSON |
Return Value:
the generated JSON response
Creates an HTTP redirect response.
public createResponseRedirect(string|\Psr\Http\Message\UriInterface $uri, bool $permanent = false, array $headers = []): \Psr\Http\Message\ResponseInterfaceThe response SHALL include a 'Location' header and appropriate status code. By default, a temporary (302) redirect is issued unless $permanent is true.
Parameters:
| Parameter | Type | Description |
|---|---|---|
$uri |
string|\Psr\Http\Message\UriInterface | the target location for the redirect |
$permanent |
bool | whether to issue a permanent (301) redirect |
$headers |
array |
Return Value:
the generated redirect response