final class Responses (View source)

Provides application controllers with consistently typed responses and one-pass streamed bodies.

Methods

Delegates response and body creation to the framework's PSR-17-backed builder.

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

Returns UTF-8 plain text with the requested status.

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

Returns UTF-8 HTML with the requested status.

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

Encodes a JSON response while preserving Unicode characters and slashes.

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

Returns an empty response carrying the target Location header.

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

Opens a file-backed response with attachment and private no-store headers.

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

Returns a one-pass response whose string-chunk producer starts on first read.

ResponseInterface

Details

__construct

__construct(ResponseBuilder $builder)

Delegates response and body creation to the framework's PSR-17-backed builder.

Parameters

ResponseBuilder $builder

text

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

Returns UTF-8 plain text with the requested status.

Parameters

string $content
int $status

Return Value

ResponseInterface

html

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

Returns UTF-8 HTML with the requested status.

Parameters

string $content
int $status

Return Value

ResponseInterface

json

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

Encodes 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)

Returns an empty response carrying the 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')

Opens a file-backed response with attachment and private no-store headers.

Parameters

string $filePath
string|null $downloadName
string $contentType

Return Value

ResponseInterface

stream

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

Returns a one-pass response whose string-chunk producer starts on first read.

Parameters

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

Return Value

ResponseInterface