final class ResponseBuilder (View source)

Builds framework HTTP responses with PSR-17 factories while preserving streaming response bodies

Methods

__construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)

Initializes the builder with PSR-17 factories for responses and bodies

ResponseInterface
text(string $content, int $status = HttpStatus::OK->value)

Creates a text response with the requested status

ResponseInterface
ResponseInterface
html(string $content, int $status = HttpStatus::OK->value)

Creates an HTML response with the requested status

ResponseInterface
ResponseInterface
json(array $payload, int $status = HttpStatus::OK->value)

Creates a JSON response while preserving Unicode characters and slashes

ResponseInterface
ResponseInterface
redirect(string $to, int $status = HttpStatus::FOUND->value)

Creates a redirect response with its target Location header

ResponseInterface
ResponseInterface
download(string $filePath, string|null $downloadName = null, string $contentType = 'application/octet-stream')

Creates a file download response with its headers and a PSR file stream body

ResponseInterface
ResponseInterface
response(string $content = '', int $status = HttpStatus::OK->value, string $contentType = 'text/html; charset=UTF-8')

Creates a response with content and a Content-Type header

ResponseInterface
ResponseInterface
stream(callable $producer, int $status = HttpStatus::OK->value, string $contentType = 'text/plain; charset=UTF-8', array $headers = [])

Creates a one-pass response whose producer lazily yields string chunks

ResponseInterface

Details

__construct

__construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)

Initializes the builder with PSR-17 factories for responses and bodies

Parameters

ResponseFactoryInterface $responseFactory
StreamFactoryInterface $streamFactory

text

ResponseInterface text(string $content, int $status = HttpStatus::OK->value)

Creates a text response with the requested status

Parameters

string $content
int $status

Return Value

ResponseInterface

html

ResponseInterface html(string $content, int $status = HttpStatus::OK->value)

Creates an HTML response with the requested status

Parameters

string $content
int $status

Return Value

ResponseInterface

json

ResponseInterface json(array $payload, int $status = HttpStatus::OK->value)

Creates a JSON response while preserving Unicode characters and slashes

Parameters

array $payload
int $status

Return Value

ResponseInterface

Exceptions

JsonException

redirect

ResponseInterface redirect(string $to, int $status = HttpStatus::FOUND->value)

Creates a redirect response with its target Location header

Parameters

string $to
int $status

Return Value

ResponseInterface

download

ResponseInterface download(string $filePath, string|null $downloadName = null, string $contentType = 'application/octet-stream')

Creates a file download response with its headers and a PSR file stream body

Parameters

string $filePath
string|null $downloadName
string $contentType

Return Value

ResponseInterface

response

ResponseInterface response(string $content = '', int $status = HttpStatus::OK->value, string $contentType = 'text/html; charset=UTF-8')

Creates a response with content and a Content-Type header

Parameters

string $content
int $status
string $contentType

Return Value

ResponseInterface

stream

ResponseInterface stream(callable $producer, int $status = HttpStatus::OK->value, string $contentType = 'text/plain; charset=UTF-8', array $headers = [])

Creates a one-pass response whose producer lazily yields string chunks

Parameters

callable $producer
int $status
string $contentType
array $headers

Return Value

ResponseInterface