WebResponse
WebResponse handles the HTTP response: status code, headers, cookies, redirects and the content sent back to the client.
Synopsis
Section titled “Synopsis”class WebResponse extends AttributeHolder implements WebResponseInterface
| Extends | AttributeHolder |
| Implements | WebResponseInterface |
| Since | 1.0.0 |
| Source | Response/WebResponse.php |
Methods
Section titled “Methods”| Method | Description |
|---|---|
__sleep(): list<string> | Pre-serialization callback. |
__wakeup(): void | Post-unserialization callback. |
addHttpHeader(string $name, mixed $value): void | |
appendContent(mixed $content): void | Append content to the existing content for this Response. |
clear(): void | Clear all response data. |
clearContent(): void | Clear the content for this Response. |
clearHttpHeaders(): void | Clears the HTTP headers set for this response. |
clearOutputType(): void | Clear the Output Type to use with this response. |
clearRedirect(): void | Clear any set redirect information. |
getContent(): mixed | Retrieve the content set for this Response. |
| [`getContentSize(): int | false`](#getcontentsize) |
getContentType(): ?string | Retrieve the content type set for the response. |
getContext(): Context | Retrieve 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: (int | string |
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(): string | Gets the HTTP status code set for the response. |
getOutputType(): ?OutputType | Get the Output Type to use with this response. |
getPsrResponse(): ?ResponseInterface | Returns the attached PSR-7 response, or null when none was attached. |
| [`getRedirect(): ?array{location: string, code: (int | string)}`](#getredirect) |
getStagedResponse(): ?ResponseInterface | The response staged by WebResponse::send(), or null if send() was never called. |
hasContent(): bool | Check whether or not some content is set. |
hasCookie(string $name): bool | Check if a cookie has been set for later sending. |
hasHttpHeader(string $name): bool | Check if an HTTP header has been set for the response. |
hasRedirect(): bool | Check if a redirect is set. |
hasStagedResponse(): bool | Whether WebResponse::send() has staged a response awaiting emission. |
initialize(Context $context, array<string, mixed> $parameters = []): void | Initialize this Response. |
isContentMutable(): bool | Determine whether the content in the response may be modified by appending or prepending data using string operations. |
merge(WebResponse $otherResponse): void | Import response metadata (attributes, headers, cookies, redirect) from another response. |
normalizeHttpHeaderName(string $name): string | Normalizes a HTTP header names |
prependContent(mixed $content): void | Prepend content to the existing content for this Response. |
removeCookie(string $name): void | Remove a cookie previously set for later sending. |
removeHttpHeader(string $name): mixed | Remove the HTTP header set for the response |
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. |
send(OutputType $outputType = null): void | Stage this response for emission. |
sendContent(): void | Send the content for this response. |
setContent(mixed $content): void | Set the content for this Response. |
setContentType(string $type): void | Set 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): void | Set a HTTP header for the response |
| [`setHttpStatusCode(string | int $code): void`](#sethttpstatuscode) |
setOutputType(OutputType $outputType): void | Set the Output Type to use with this response. |
setPsrResponse(?ResponseInterface $psr): void | Attach a PSR-7 response instance for forwarding. |
| [`setRedirect(mixed $location, int | string $code = 302): void`](#setredirect) |
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. |
unsetCookie(string $name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null): void | Unset an existing cookie. |
| [`validateHttpStatusCode(string | int $code): bool`](#validatehttpstatuscode) |
__sleep()
Section titled “__sleep()”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``>
__wakeup()
Section titled “__wakeup()”public function __wakeup(): void
Post-unserialization callback.
Restores the Context, the OutputType and the content stream from the identifiers recorded by __sleep().
addHttpHeader()
Section titled “addHttpHeader()”public function addHttpHeader(string $name, mixed $value): void
A HTTP header field value, or an array of values.
| Parameter | Type | Description |
|---|---|---|
$name | string | A HTTP header field name. |
$value | mixed | A HTTP header field value, or an array of values. |
appendContent()
Section titled “appendContent()”public function appendContent(mixed $content): void
Append content to the existing content for this Response.
The content to be appended to this Response.
| Parameter | Type | Description |
|---|---|---|
$content | mixed | The content to be appended to this Response. |
clear()
Section titled “clear()”public function clear(): void
Clear all response data.
clearContent()
Section titled “clearContent()”public function clearContent(): void
Clear the content for this Response.
clearHttpHeaders()
Section titled “clearHttpHeaders()”public function clearHttpHeaders(): void
Clears the HTTP headers set for this response.
clearOutputType()
Section titled “clearOutputType()”public function clearOutputType(): void
Clear the Output Type to use with this response.
clearRedirect()
Section titled “clearRedirect()”public function clearRedirect(): void
Clear any set redirect information.
getContent()
Section titled “getContent()”public function getContent(): mixed
Retrieve the content set for this Response.
Returns mixed — The content set in this Response.
getContentSize()
Section titled “getContentSize()”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.
getContentType()
Section titled “getContentType()”public function getContentType(): ?string
Retrieve the content type set for the response.
Returns ?``string — A content type, or null if none is set.
getContext()
Section titled “getContext()”final public function getContext(): Context
Retrieve the Context instance this Response object belongs to.
Returns Context — An Context instance.
| Throws | When |
|---|---|
InitializationException | If this Response has not been initialized yet. |
getCookie()
Section titled “getCookie()”public function getCookie(string $name): ?array<string, mixed>
Get a cookie set for later sending.
The name of the cookie.
| Parameter | Type | Description |
|---|---|---|
$name | string | The 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.
getCookies()
Section titled “getCookies()”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).
getHttpHeader()
Section titled “getHttpHeader()”public function getHttpHeader(string $name): ?list<string>
Retrieve the HTTP header values set for the response.
A HTTP header field name.
| Parameter | Type | Description |
|---|---|---|
$name | string | A HTTP header field name. |
Returns ?``list``<``string``> — All values set for that header, or null if no headers set
getHttpHeaders()
Section titled “getHttpHeaders()”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.
getHttpStatusCode()
Section titled “getHttpStatusCode()”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.
getOutputType()
Section titled “getOutputType()”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.
getPsrResponse()
Section titled “getPsrResponse()”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
getRedirect()
Section titled “getRedirect()”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.
getStagedResponse()
Section titled “getStagedResponse()”public function getStagedResponse(): ?ResponseInterface
The response staged by WebResponse::send(), or null if send() was never called.
Returns ?ResponseInterface
hasContent()
Section titled “hasContent()”public function hasContent(): bool
Check whether or not some content is set.
Returns bool — If any content is set, false otherwise.
hasCookie()
Section titled “hasCookie()”public function hasCookie(string $name): bool
Check if a cookie has been set for later sending.
The name of the cookie.
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the cookie. |
Returns bool — True if a cookie with that name has been set, else false.
hasHttpHeader()
Section titled “hasHttpHeader()”public function hasHttpHeader(string $name): bool
Check if an HTTP header has been set for the response.
A HTTP header field name.
| Parameter | Type | Description |
|---|---|---|
$name | string | A HTTP header field name. |
Returns bool — true if the header exists, false otherwise.
hasRedirect()
Section titled “hasRedirect()”public function hasRedirect(): bool
Check if a redirect is set.
Returns bool — true, if a redirect is set, otherwise false
hasStagedResponse()
Section titled “hasStagedResponse()”public function hasStagedResponse(): bool
Whether WebResponse::send() has staged a response awaiting emission.
Returns bool
initialize()
Section titled “initialize()”public function initialize(Context $context, array<string, mixed> $parameters = []): void
Initialize this Response.
An array of initialization parameters.
| Parameter | Type | Description |
|---|---|---|
$context | Context | An Context instance. |
$parameters | array``<``string``, ``mixed``> | An array of initialization parameters. |
isContentMutable()
Section titled “isContentMutable()”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.
merge()
Section titled “merge()”public function merge(WebResponse $otherResponse): void
Import response metadata (attributes, headers, cookies, redirect) from another response.
The other response to import information from.
| Parameter | Type | Description |
|---|---|---|
$otherResponse | WebResponse | The other response to import information from. |
normalizeHttpHeaderName()
Section titled “normalizeHttpHeaderName()”public function normalizeHttpHeaderName(string $name): string
Normalizes a HTTP header names
A HTTP header name
| Parameter | Type | Description |
|---|---|---|
$name | string | A HTTP header name |
Returns string — A normalized HTTP header name
prependContent()
Section titled “prependContent()”public function prependContent(mixed $content): void
Prepend content to the existing content for this Response.
The content to be prepended to this Response.
| Parameter | Type | Description |
|---|---|---|
$content | mixed | The content to be prepended to this Response. |
removeCookie()
Section titled “removeCookie()”public function removeCookie(string $name): void
Remove a cookie previously set for later sending.
The name of the cookie.
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the cookie. |
removeHttpHeader()
Section titled “removeHttpHeader()”public function removeHttpHeader(string $name): mixed
Remove the HTTP header set for the response
A HTTP header field name.
| Parameter | Type | Description |
|---|---|---|
$name | string | A HTTP header field name. |
Returns mixed — The removed header’s value or null if header was not set.
reset()
Section titled “reset()”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.
send()
Section titled “send()”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
| Parameter | Type | Description |
|---|---|---|
$outputType | OutputType | An optional Output Type object with information the response can use to send additional data, such as HTTP headers |
sendContent()
Section titled “sendContent()”public function sendContent(): void
Send the content for this response.
setContent()
Section titled “setContent()”public function setContent(mixed $content): void
Set the content for this Response.
The content to be sent in this Response.
| Parameter | Type | Description |
|---|---|---|
$content | mixed | The content to be sent in this Response. |
setContentType()
Section titled “setContentType()”public function setContentType(string $type): void
Set the content type for the response.
A content type.
| Parameter | Type | Description |
|---|---|---|
$type | string | A content type. |
setCookie()
Section titled “setCookie()”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.
| Parameter | Type | Description |
|---|---|---|
$name | string | A cookie name. |
$value | mixed | Data to store into a cookie. If null or empty cookie will be tried to be removed. |
$lifetime | mixed | The 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 | ?``string | The path on the server the cookie will be available on. |
$domain | ?``string | The domain the cookie is available on. |
$secure | ?``bool | Indicates that the cookie should only be transmitted over a secure HTTPS connection. |
$httponly | ?``bool | Whether the cookie will be made accessible only through the HTTP protocol, and not to client-side scripts. |
$encodeCallback | mixed | Callback to encode the cookie value. Set to false if you did already encode the value on your own. |
$samesite | ?``string | The SameSite attribute for the cookie. |
setHttpHeader()
Section titled “setHttpHeader()”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.
| Parameter | Type | Description |
|---|---|---|
$name | string | A HTTP header field name. |
$value | mixed | A HTTP header field value, of an array of values. |
$replace | bool | If true, a header with that name will be overwritten, otherwise, the value will be appended. |
setHttpStatusCode()
Section titled “setHttpStatusCode()”public function setHttpStatusCode(string|int $code): void
Sets a HTTP status code for the response.
A numeric HTTP status code.
| Parameter | Type | Description |
|---|---|---|
$code | `string“ | “int` |
setOutputType()
Section titled “setOutputType()”public function setOutputType(OutputType $outputType): void
Set the Output Type to use with this response.
| Parameter | Type | Description |
|---|---|---|
$outputType | OutputType |
setPsrResponse()
Section titled “setPsrResponse()”public function setPsrResponse(?ResponseInterface $psr): void
Attach a PSR-7 response instance for forwarding.
| Parameter | Type | Description |
|---|---|---|
$psr | ?ResponseInterface |
setRedirect()
Section titled “setRedirect()”public function setRedirect(mixed $location, int|string $code = 302): void
Redirect externally.
A numeric HTTP status code.
| Parameter | Type | Description |
|---|---|---|
$location | mixed | Where to redirect. |
$code | `int“ | “string` |
toPsrResponse()
Section titled “toPsrResponse()”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.
| Parameter | Type | Description |
|---|---|---|
$outputType | ?OutputType | Output type whose http_headers to fold in; defaults to this response’s own. |
Returns ResponseInterface
| Throws | When |
|---|---|
QuioteException | If a relative redirect is set with no initialized Context. |
unsetCookie()
Section titled “unsetCookie()”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.
| Parameter | Type | Description |
|---|---|---|
$name | string | A cookie name. |
$path | string | The path on the server the cookie will be available on. |
$domain | string | The domain the cookie is available on. |
$secure | bool | Indicates that the cookie should only be transmitted over a secure HTTPS connection. |
$httponly | bool | Whether the cookie will be made accessible only through the HTTP protocol, and not to client-side scripts. |
validateHttpStatusCode()
Section titled “validateHttpStatusCode()”public function validateHttpStatusCode(string|int $code): bool
Check if the given HTTP status code is valid.
A numeric HTTP status code.
| Parameter | Type | Description |
|---|---|---|
$code | `string“ | “int` |
Returns bool — True, if the code is valid, or false otherwise.
Inherited methods
Section titled “Inherited methods”These come from an ancestor and are documented where they are declared.
| Method | Declared in | Description |
|---|---|---|
appendAttribute() | AttributeHolder | Append an attribute. |
appendAttributeByRef() | AttributeHolder | Append an attribute by reference. |
appendParameter() | ParameterHolder | Append a parameter. |
appendParameterByRef() | ParameterHolder | Append a parameter by reference. |
clearAttributes() | AttributeHolder | Clear all attributes. |
clearParameters() | ParameterHolder | Clear all parameters associated with this request. |
getAttribute() | AttributeHolder | Retrieve an attribute. |
getAttributeNames() | AttributeHolder | Retrieve an array of attribute names. |
getAttributeNamespace() | AttributeHolder | Retrieve all attributes within a namespace. |
getAttributeNamespaces() | AttributeHolder | Retrieve an array of attribute namespaces. |
getAttributes() | AttributeHolder | Retrieve all attributes within a namespace. |
getDefaultNamespace() | AttributeHolder | Get the default namespace name |
getFlatAttributeNames() | AttributeHolder | Retrieve an array of flattened attribute names. |
getFlatParameterNames() | ParameterHolder | Retrieve an array of flattened parameter names. |
getParameter() | ParameterHolder | Retrieve a parameter. |
getParameterNames() | ParameterHolder | Retrieve an array of parameter names. |
getParameters() | ParameterHolder | Retrieve an array of parameters. |
hasAttribute() | AttributeHolder | Indicates whether or not an attribute exists. |
hasAttributeNamespace() | AttributeHolder | Indicates whether or not an attribute namespace exists. |
hasParameter() | ParameterHolder | Indicates whether or not a parameter exists. |
removeAttribute() | AttributeHolder | Remove an attribute. |
removeAttributeNamespace() | AttributeHolder | Remove an attribute namespace and all of its associated attributes. |
removeParameter() | ParameterHolder | Remove a parameter. |
setAttribute() | AttributeHolder | Set an attribute. |
setAttributeByRef() | AttributeHolder | Set an attribute by reference. |
setAttributes() | AttributeHolder | Set an array of attributes. |
setAttributesByRef() | AttributeHolder | Set an array of attributes by reference. |
setParameter() | ParameterHolder | Set a parameter. |
setParameterByRef() | ParameterHolder | Set a parameter by reference. |
setParameters() | ParameterHolder | Set an array of parameters. |
setParametersByRef() | ParameterHolder | Set an array of parameters by reference. |