ResponseFactory

Class
Read only: Yes Final: Yes

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.

Table of Contents

Interfaces

ResponseFactoryInterface

Interface ResponseFactoryInterface.

Properties

 : ResponseFactoryInterface

Methods

__construct()

Constructs the ResponseFactory.

 : mixed
createResponse()

Creates a standard HTTP response.

 : ResponseInterface
createResponseFromHtml()

Creates an HTTP response containing HTML content.

 : ResponseInterface
createResponseFromPayload()

Creates an HTTP response containing a JSON-encoded payload.

 : PayloadResponseInterface
createResponseFromText()

Creates an HTTP response containing plain text content.

 : ResponseInterface
createResponseNoContent()

Creates an HTTP 204 No Content response.

 : ResponseInterface
createResponseRedirect()

Creates an HTTP redirect response.

 : ResponseInterface
Properties

$responseFactory

Private
private ResponseFactoryInterface $responseFactory
Methods

__construct()

Public

Constructs the ResponseFactory.

public __construct(ResponseFactoryInterface  $responseFactory) : mixed
Parameters
$responseFactory : ResponseFactoryInterface

Description

the underlying PSR-17 response factory implementation

createResponse()

Public

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
ResponseInterface

Description

the generated response

createResponseFromHtml()

Public

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
ResponseInterface

Description

the generated HTML response

createResponseFromPayload()

Public

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
PayloadResponseInterface

Description

the generated JSON response

createResponseFromText()

Public

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
ResponseInterface

Description

the generated plain text response

createResponseNoContent()

Public

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
ResponseInterface

Description

the generated no content response

createResponseRedirect()

Public

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
ResponseInterface

Description

the generated redirect response