Skip to content

WebResponse

WebResponse handles the HTTP response: status code, headers, cookies, redirects and the content sent back to the client.

class WebResponse extends AttributeHolder implements WebResponseInterface

ExtendsAttributeHolder
ImplementsWebResponseInterface
Since1.0.0
SourceResponse/WebResponse.php
MethodDescription
__sleep(): list<string>Pre-serialization callback.
__wakeup(): voidPost-unserialization callback.
addHttpHeader(string $name, mixed $value): void
appendContent(mixed $content): voidAppend content to the existing content for this Response.
clear(): voidClear all response data.
clearContent(): voidClear the content for this Response.
clearHttpHeaders(): voidClears the HTTP headers set for this response.
clearOutputType(): voidClear the Output Type to use with this response.
clearRedirect(): voidClear any set redirect information.
getContent(): mixedRetrieve the content set for this Response.
[`getContentSize(): intfalse`](#getcontentsize)
getContentType(): ?stringRetrieve the content type set for the response.
getContext(): ContextRetrieve the Context instance this Response object belongs to.
getCookie(string $name): ?array<string, mixed>Get a cookie set for later sending.
[`getCookies(): array<string, array{value: mixed, lifetime: (intstring
getHttpHeader(string $name): ?list<string>Retrieve the HTTP header values set for the response.
getHttpHeaders(): array<string, list<string>>Retrieve the HTTP headers set for the response.
getHttpStatusCode(): stringGets the HTTP status code set for the response.
getOutputType(): ?OutputTypeGet the Output Type to use with this response.
getPsrResponse(): ?ResponseInterfaceReturns the attached PSR-7 response, or null when none was attached.
[`getRedirect(): ?array{location: string, code: (intstring)}`](#getredirect)
getStagedResponse(): ?ResponseInterfaceThe response staged by WebResponse::send(), or null if send() was never called.
hasContent(): boolCheck whether or not some content is set.
hasCookie(string $name): boolCheck if a cookie has been set for later sending.
hasHttpHeader(string $name): boolCheck if an HTTP header has been set for the response.
hasRedirect(): boolCheck if a redirect is set.
hasStagedResponse(): boolWhether WebResponse::send() has staged a response awaiting emission.
initialize(Context $context, array<string, mixed> $parameters = []): voidInitialize this Response.
isContentMutable(): boolDetermine whether the content in the response may be modified by appending or prepending data using string operations.
merge(WebResponse $otherResponse): voidImport response metadata (attributes, headers, cookies, redirect) from another response.
normalizeHttpHeaderName(string $name): stringNormalizes a HTTP header names
prependContent(mixed $content): voidPrepend content to the existing content for this Response.
removeCookie(string $name): voidRemove a cookie previously set for later sending.
removeHttpHeader(string $name): mixedRemove the HTTP header set for the response
reset(): voidReset response state for worker compatibility: everything a request can put on the response has to go, or request N’s body/headers/cookies would bleed into request N+1.
send(OutputType $outputType = null): voidStage this response for emission.
sendContent(): voidSend the content for this response.
setContent(mixed $content): voidSet the content for this Response.
setContentType(string $type): voidSet the content type for the response.
setCookie(string $name, mixed $value, mixed $lifetime = null, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, mixed $encodeCallback = null, ?string $samesite = null): void
setHttpHeader(string $name, mixed $value, bool $replace = true): voidSet a HTTP header for the response
[`setHttpStatusCode(stringint $code): void`](#sethttpstatuscode)
setOutputType(OutputType $outputType): voidSet the Output Type to use with this response.
setPsrResponse(?ResponseInterface $psr): voidAttach a PSR-7 response instance for forwarding.
[`setRedirect(mixed $location, intstring $code = 302): void`](#setredirect)
toPsrResponse(?OutputType $outputType = null): ResponseInterfaceMaterialize this response as PSR-7: status, prepared headers, queued cookies and body, with no side effects on any output channel.
unsetCookie(string $name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null): voidUnset an existing cookie.
[`validateHttpStatusCode(stringint $code): bool`](#validatehttpstatuscode)

public function __sleep(): list<string>

Pre-serialization callback.

Heavy object references (the Context, the OutputType) and the content stream cannot be serialized, so record the identifiers needed to look them back up in __wakeup() and leave the objects themselves out.

Returns list``<``string``>

public function __wakeup(): void

Post-unserialization callback.

Restores the Context, the OutputType and the content stream from the identifiers recorded by __sleep().

public function addHttpHeader(string $name, mixed $value): void

A HTTP header field value, or an array of values.

ParameterTypeDescription
$namestringA HTTP header field name.
$valuemixedA HTTP header field value, or an array of values.

public function appendContent(mixed $content): void

Append content to the existing content for this Response.

The content to be appended to this Response.

ParameterTypeDescription
$contentmixedThe content to be appended to this Response.

public function clear(): void

Clear all response data.

public function clearContent(): void

Clear the content for this Response.

public function clearHttpHeaders(): void

Clears the HTTP headers set for this response.

public function clearOutputType(): void

Clear the Output Type to use with this response.

public function clearRedirect(): void

Clear any set redirect information.

public function getContent(): mixed

Retrieve the content set for this Response.

Returns mixed — The content set in this Response.

public function getContentSize(): int|false

Retrieve the size (in bytes) of the content set for this Response.

Returns int``|``false — The content size in bytes, or false if it could not be determined.

public function getContentType(): ?string

Retrieve the content type set for the response.

Returns ?``string — A content type, or null if none is set.

final public function getContext(): Context

Retrieve the Context instance this Response object belongs to.

Returns Context — An Context instance.

ThrowsWhen
InitializationExceptionIf this Response has not been initialized yet.

public function getCookie(string $name): ?array<string, mixed>

Get a cookie set for later sending.

The name of the cookie.

ParameterTypeDescription
$namestringThe name of the cookie.

Returns ?``array``<``string``, ``mixed``> — An associative array containing the cookie data or null if no cookie with that name has been set.

public function getCookies(): array<string, array{value: mixed, lifetime: (int | string | null), path: (string | null), domain: (string | null), secure: bool, httponly: bool, encode_callback: (callable | false), samesite: (string | null)}>

Get a list of cookies set for later sending.

Returns array``<``string``, ``array{value: mixed, lifetime: (int | string | null), path: (string | null), domain: (string | null), secure: bool, httponly: bool, encode_callback: (callable | false), samesite: (string | null)}``> — An associative array of cookie names (key) and cookie information (value, associative array).

public function getHttpHeader(string $name): ?list<string>

Retrieve the HTTP header values set for the response.

A HTTP header field name.

ParameterTypeDescription
$namestringA HTTP header field name.

Returns ?``list``<``string``> — All values set for that header, or null if no headers set

public function getHttpHeaders(): array<string, list<string>>

Retrieve the HTTP headers set for the response.

Returns array``<``string``, ``list``<``string``>``> — An associative array of HTTP header names and values.

public function getHttpStatusCode(): string

Gets the HTTP status code set for the response.

Returns string — A numeric HTTP status code between 100 and 505, or null if no status code has been set.

public function getOutputType(): ?OutputType

Get the Output Type to use with this response.

Returns ?OutputType — The Output Type instance associated with, or null if none is set.

public function getPsrResponse(): ?ResponseInterface

Returns the attached PSR-7 response, or null when none was attached.

Not a snapshot of this response: it is the instance handed to WebResponse::setPsrResponse(), replaced in place as status codes, headers and cookies set on this response are mirrored onto it. It is null until a response is attached, and again after WebResponse::reset().

Returns ?ResponseInterface

public function getRedirect(): ?array{location: string, code: (int | string)}

Get info about the set redirect.

Returns ?``array{location: string, code: (int | string)} — An assoc array of redirect info, or null if none set.

public function getStagedResponse(): ?ResponseInterface

The response staged by WebResponse::send(), or null if send() was never called.

Returns ?ResponseInterface

public function hasContent(): bool

Check whether or not some content is set.

Returns bool — If any content is set, false otherwise.

public function hasCookie(string $name): bool

Check if a cookie has been set for later sending.

The name of the cookie.

ParameterTypeDescription
$namestringThe name of the cookie.

Returns bool — True if a cookie with that name has been set, else false.

public function hasHttpHeader(string $name): bool

Check if an HTTP header has been set for the response.

A HTTP header field name.

ParameterTypeDescription
$namestringA HTTP header field name.

Returns bool — true if the header exists, false otherwise.

public function hasRedirect(): bool

Check if a redirect is set.

Returns bool — true, if a redirect is set, otherwise false

public function hasStagedResponse(): bool

Whether WebResponse::send() has staged a response awaiting emission.

Returns bool

public function initialize(Context $context, array<string, mixed> $parameters = []): void

Initialize this Response.

An array of initialization parameters.

ParameterTypeDescription
$contextContextAn Context instance.
$parametersarray``<``string``, ``mixed``>An array of initialization parameters.

public function isContentMutable(): bool

Determine whether the content in the response may be modified by appending or prepending data using string operations.

Typically false for streams or responses where the content is not a string (e.g. an array).

Returns bool — If the content can be treated as / changed like a string.

public function merge(WebResponse $otherResponse): void

Import response metadata (attributes, headers, cookies, redirect) from another response.

The other response to import information from.

ParameterTypeDescription
$otherResponseWebResponseThe other response to import information from.

public function normalizeHttpHeaderName(string $name): string

Normalizes a HTTP header names

A HTTP header name

ParameterTypeDescription
$namestringA HTTP header name

Returns string — A normalized HTTP header name

public function prependContent(mixed $content): void

Prepend content to the existing content for this Response.

The content to be prepended to this Response.

ParameterTypeDescription
$contentmixedThe content to be prepended to this Response.

public function removeCookie(string $name): void

Remove a cookie previously set for later sending.

The name of the cookie.

ParameterTypeDescription
$namestringThe name of the cookie.

public function removeHttpHeader(string $name): mixed

Remove the HTTP header set for the response

A HTTP header field name.

ParameterTypeDescription
$namestringA HTTP header field name.

Returns mixed — The removed header’s value or null if header was not set.

public function reset(): void

Reset response state for worker compatibility: everything a request can put on the response has to go, or request N’s body/headers/cookies would bleed into request N+1.

The Context is deliberately kept — it is application-scoped, not request-scoped, and a reused response instance is not re-initialize()d before the next request.

public function send(OutputType $outputType = null): void

Stage this response for emission.

An optional Output Type object with information the response can use to send additional data, such as HTTP headers

ParameterTypeDescription
$outputTypeOutputTypeAn optional Output Type object with information the response can use to send additional data, such as HTTP headers

public function sendContent(): void

Send the content for this response.

public function setContent(mixed $content): void

Set the content for this Response.

The content to be sent in this Response.

ParameterTypeDescription
$contentmixedThe content to be sent in this Response.

public function setContentType(string $type): void

Set the content type for the response.

A content type.

ParameterTypeDescription
$typestringA content type.

public function setCookie(string $name, mixed $value, mixed $lifetime = null, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, mixed $encodeCallback = null, ?string $samesite = null): void

The SameSite attribute for the cookie.

ParameterTypeDescription
$namestringA cookie name.
$valuemixedData to store into a cookie. If null or empty cookie will be tried to be removed.
$lifetimemixedThe lifetime of the cookie in seconds. When you pass 0 the cookie will be valid until the browser is closed. You can also use a strtotime() string instead of an int.
$path?``stringThe path on the server the cookie will be available on.
$domain?``stringThe domain the cookie is available on.
$secure?``boolIndicates that the cookie should only be transmitted over a secure HTTPS connection.
$httponly?``boolWhether the cookie will be made accessible only through the HTTP protocol, and not to client-side scripts.
$encodeCallbackmixedCallback to encode the cookie value. Set to false if you did already encode the value on your own.
$samesite?``stringThe SameSite attribute for the cookie.

public function setHttpHeader(string $name, mixed $value, bool $replace = true): void

Set a HTTP header for the response

If true, a header with that name will be overwritten, otherwise, the value will be appended.

ParameterTypeDescription
$namestringA HTTP header field name.
$valuemixedA HTTP header field value, of an array of values.
$replaceboolIf true, a header with that name will be overwritten, otherwise, the value will be appended.

public function setHttpStatusCode(string|int $code): void

Sets a HTTP status code for the response.

A numeric HTTP status code.

ParameterTypeDescription
$code`string““int`

public function setOutputType(OutputType $outputType): void

Set the Output Type to use with this response.

ParameterTypeDescription
$outputTypeOutputType

public function setPsrResponse(?ResponseInterface $psr): void

Attach a PSR-7 response instance for forwarding.

ParameterTypeDescription
$psr?ResponseInterface

public function setRedirect(mixed $location, int|string $code = 302): void

Redirect externally.

A numeric HTTP status code.

ParameterTypeDescription
$locationmixedWhere to redirect.
$code`int““string`

public function toPsrResponse(?OutputType $outputType = null): ResponseInterface

Materialize this response as PSR-7: status, prepared headers, queued cookies and body, with no side effects on any output channel.

Output type whose http_headers to fold in; defaults to this response’s own.

ParameterTypeDescription
$outputType?OutputTypeOutput type whose http_headers to fold in; defaults to this response’s own.

Returns ResponseInterface

ThrowsWhen
QuioteExceptionIf a relative redirect is set with no initialized Context.

public function unsetCookie(string $name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null): void

Unset an existing cookie.

Whether the cookie will be made accessible only through the HTTP protocol, and not to client-side scripts.

ParameterTypeDescription
$namestringA cookie name.
$pathstringThe path on the server the cookie will be available on.
$domainstringThe domain the cookie is available on.
$secureboolIndicates that the cookie should only be transmitted over a secure HTTPS connection.
$httponlyboolWhether the cookie will be made accessible only through the HTTP protocol, and not to client-side scripts.

public function validateHttpStatusCode(string|int $code): bool

Check if the given HTTP status code is valid.

A numeric HTTP status code.

ParameterTypeDescription
$code`string““int`

Returns bool — True, if the code is valid, or false otherwise.

These come from an ancestor and are documented where they are declared.

MethodDeclared inDescription
appendAttribute()AttributeHolderAppend an attribute.
appendAttributeByRef()AttributeHolderAppend an attribute by reference.
appendParameter()ParameterHolderAppend a parameter.
appendParameterByRef()ParameterHolderAppend a parameter by reference.
clearAttributes()AttributeHolderClear all attributes.
clearParameters()ParameterHolderClear all parameters associated with this request.
getAttribute()AttributeHolderRetrieve an attribute.
getAttributeNames()AttributeHolderRetrieve an array of attribute names.
getAttributeNamespace()AttributeHolderRetrieve all attributes within a namespace.
getAttributeNamespaces()AttributeHolderRetrieve an array of attribute namespaces.
getAttributes()AttributeHolderRetrieve all attributes within a namespace.
getDefaultNamespace()AttributeHolderGet the default namespace name
getFlatAttributeNames()AttributeHolderRetrieve an array of flattened attribute names.
getFlatParameterNames()ParameterHolderRetrieve an array of flattened parameter names.
getParameter()ParameterHolderRetrieve a parameter.
getParameterNames()ParameterHolderRetrieve an array of parameter names.
getParameters()ParameterHolderRetrieve an array of parameters.
hasAttribute()AttributeHolderIndicates whether or not an attribute exists.
hasAttributeNamespace()AttributeHolderIndicates whether or not an attribute namespace exists.
hasParameter()ParameterHolderIndicates whether or not a parameter exists.
removeAttribute()AttributeHolderRemove an attribute.
removeAttributeNamespace()AttributeHolderRemove an attribute namespace and all of its associated attributes.
removeParameter()ParameterHolderRemove a parameter.
setAttribute()AttributeHolderSet an attribute.
setAttributeByRef()AttributeHolderSet an attribute by reference.
setAttributes()AttributeHolderSet an array of attributes.
setAttributesByRef()AttributeHolderSet an array of attributes by reference.
setParameter()ParameterHolderSet a parameter.
setParameterByRef()ParameterHolderSet a parameter by reference.
setParameters()ParameterHolderSet an array of parameters.
setParametersByRef()ParameterHolderSet an array of parameters by reference.