Class ResponseFactory.
Description
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.
Interfaces
Properties
Methods
Constructs the ResponseFactory.
Creates a standard HTTP response.
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.
Constructs the ResponseFactory.
public
__construct(ResponseFactoryInterface
$responseFactory) : mixed
Parameters
$responseFactory
:
ResponseFactoryInterface
Description
the underlying PSR-17 response factory implementation
Creates a standard HTTP response.
public
createResponse([
int
$code = 200][,
string
$reasonPhrase = '']) : ResponseInterface
Parameters
$code
:
int
=
200
Description
The HTTP status code. Defaults to 200 (OK).
$reasonPhrase
:
string
=
''
Description
optional reason phrase
Return values
Description
the generated response
Creates an HTTP response containing HTML content.
public
createResponseFromHtml(
string
$html) : ResponseInterface
Description
The response SHALL 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 SHALL 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 SHALL 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
This response SHALL contain no body and 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 = false][,
array<string|int, mixed>
$headers = []]) : ResponseInterface
Description
The response SHALL include a 'Location' header and appropriate status code. By default, a temporary (302) redirect is issued unless $permanent is true.
Parameters
$uri
:
string|UriInterface
Description
the target location for the redirect
$permanent
:
bool
=
false
Description
whether to issue a permanent (301) redirect
$headers
:
array<string|int, mixed>
=
[]
Return values
Description
the generated redirect response