ResponseFactoryInterface
Interface ResponseFactoryInterface.
Description
Extends PSR-17 ResponseFactoryInterface and defines additional convenience methods for generating common types of HTTP responses, including HTML, plain text, JSON payloads, redirects, and empty responses.
Implementations of this interface MUST comply with the method requirements and MUST return immutable instances as per PSR-7 standards.
Methods
Creates an HTTP response containing HTML content.
Creates an HTTP response containing a JSON-encoded payload.
Creates an HTTP response containing plain text content.
Creates an HTTP 204 No Content response.
Creates an HTTP redirect response.
Creates an HTTP response containing HTML content.
public
createResponseFromHtml(
string
$html) : ResponseInterface
Description
The response MUST have 'Content-Type: text/html' set automatically.
Parameters
$html
:
string
Description
the HTML content to include in the response body
Return values
Description
the generated HTML response
Creates an HTTP response containing a JSON-encoded payload.
public
createResponseFromPayload(
array<string|int, mixed>
$payload) : PayloadResponseInterface
Description
The response MUST have 'Content-Type: application/json' set automatically.
Parameters
$payload
:
array<string|int, mixed>
Description
the payload to encode as JSON
Return values
Description
the generated JSON response
Creates an HTTP response containing plain text content.
public
createResponseFromText(
string
$text) : ResponseInterface
Description
The response MUST have 'Content-Type: text/plain' set automatically.
Parameters
$text
:
string
Description
the plain text content to include in the response body
Return values
Description
the generated plain text response
Creates an HTTP 204 No Content response.
public
createResponseNoContent(
array<string|int, mixed>
$headers) : ResponseInterface
Description
The response MUST contain no body and MUST have status code 204.
Parameters
$headers
:
array<string|int, mixed>
Return values
Description
the generated no content response
Creates an HTTP redirect response.
public
createResponseRedirect(
string|UriInterface
$uri,
bool
$permanent,
array<string|int, mixed>
$headers) : ResponseInterface
Description
The response MUST include a 'Location' header and an appropriate status code: 301 (permanent) if $permanent is true, or 302 (temporary) otherwise.
Parameters
$uri
:
string|UriInterface
Description
the target location for the redirect
$permanent
:
bool
Description
if true, issues a permanent redirect; otherwise, temporary
$headers
:
array<string|int, mixed>
Return values
Description
the generated redirect response