ResponseFactoryInterface

Interface
extends 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.

Table of Contents

Methods

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
Methods

createResponseFromHtml()

Public

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
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 MUST 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 MUST 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

The response MUST contain no body and MUST 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, 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
ResponseInterface

Description

the generated redirect response