Skip to content

ResponseFactory

github-actions edited this page Mar 29, 2026 · 1 revision

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.


Methods

__construct

Constructs the ResponseFactory.

public __construct(\Psr\Http\Message\ResponseFactoryInterface $responseFactory): mixed

Parameters:

Parameter Type Description
$responseFactory \Psr\Http\Message\ResponseFactoryInterface the underlying PSR-17 response factory implementation

createResponse

Creates a standard HTTP response.

public createResponse(int $code = 200, string $reasonPhrase = ''): \Psr\Http\Message\ResponseInterface

Parameters:

Parameter Type Description
$code int The HTTP status code. Defaults to 200 (OK).
$reasonPhrase string optional reason phrase

Return Value:

the generated response


createResponseFromHtml

Creates an HTTP response containing HTML content.

public createResponseFromHtml(string $html): \Psr\Http\Message\ResponseInterface

The 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


createResponseFromText

Creates an HTTP response containing plain text content.

public createResponseFromText(string $text): \Psr\Http\Message\ResponseInterface

The 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


createResponseNoContent

Creates an HTTP 204 No Content response.

public createResponseNoContent(array $headers = []): \Psr\Http\Message\ResponseInterface

This response SHALL contain no body and have status code 204.

Parameters:

Parameter Type Description
$headers array

Return Value:

the generated no content response


createResponseFromPayload

Creates an HTTP response containing a JSON-encoded payload.

public createResponseFromPayload(array $payload): \FastForward\Http\Message\PayloadResponseInterface

The 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


createResponseRedirect

Creates an HTTP redirect response.

public createResponseRedirect(string|\Psr\Http\Message\UriInterface $uri, bool $permanent = false, array $headers = []): \Psr\Http\Message\ResponseInterface

The 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


Clone this wiki locally