final class Response implements IResponse

Mutable HTTP response for setting status code, headers, cookies, and redirects.

Properties

$cookieDomain

The domain in which the cookie will be available

$cookiePath

The path in which the cookie will be available

$cookieSecure

Whether the cookie is available only through HTTPS

$warnOnBuffer

Whether to warn when there is data in the output buffer before sending headers

array<string,string> read-only $headers

Methods

__construct()

No description

setCode(int $code, string|null $reason = null)

Sets HTTP response code.

int
getCode()

Returns HTTP response code.

setHeader(string $name, string $value)

Sends an HTTP header, replacing any previously sent header with the same name.

addHeader(string $name, string $value)

Adds an HTTP header without replacing a previously sent header with the same name.

self
deleteHeader(string $name)

Deletes a previously sent HTTP header.

setContentType(string $type, string|null $charset = null)

Sends a Content-type HTTP header.

sendAsFile(string $fileName)

Triggers a browser download dialog for the response body with the given filename.

void
redirect(string $url, int $code = self::S302_Found)

Redirects to another URL. Don't forget to quit the script then.

setExpiration(string|null $expire)

Sets the Cache-Control and Expires headers. Pass a time string (e.g. '20 minutes') to enable caching, or null to disable it entirely.

bool
isSent()

Checks whether HTTP headers have already been sent, making it impossible to modify them.

string|null
getHeader(string $header)

Returns the sent HTTP header, or null if it does not exist. The parameter is case-insensitive.

array
getHeaders()

Returns all sent HTTP headers as associative array.

setCookie(string $name, string $value, string|int|DateTimeInterface|null $expire, string|null $path = null, string|null $domain = null, bool $secure = null, bool $httpOnly = true, SameSite $sameSite = SameSite::Lax, bool $partitioned = false)

Sends a cookie.

void
deleteCookie(string $name, string|null $path = null, string|null $domain = null, bool $secure = null)

Deletes a cookie.

Details

at line 40
__construct()

No description

at line 53
IResponse setCode(int $code, string|null $reason = null)

Sets HTTP response code.

Parameters

int $code
string|null $reason

Return Value

IResponse

Exceptions

InvalidArgumentException
InvalidStateException

at line 71
int getCode()

Returns HTTP response code.

Return Value

int

at line 81
IResponse setHeader(string $name, string $value)

Sends an HTTP header, replacing any previously sent header with the same name.

Parameters

string $name
string $value

Return Value

IResponse

Exceptions

InvalidStateException

at line 100
IResponse addHeader(string $name, string $value)

Adds an HTTP header without replacing a previously sent header with the same name.

Parameters

string $name
string $value

Return Value

IResponse

Exceptions

InvalidStateException

at line 112
self deleteHeader(string $name)

Deletes a previously sent HTTP header.

Parameters

string $name

Return Value

self

Exceptions

InvalidStateException

at line 124
IResponse setContentType(string $type, string|null $charset = null)

Sends a Content-type HTTP header.

Parameters

string $type
string|null $charset

Return Value

IResponse

Exceptions

InvalidStateException

at line 135
Response sendAsFile(string $fileName)

Triggers a browser download dialog for the response body with the given filename.

Parameters

string $fileName

Return Value

Response

Exceptions

InvalidStateException

at line 150
void redirect(string $url, int $code = self::S302_Found)

Redirects to another URL. Don't forget to quit the script then.

Parameters

string $url
int $code

Return Value

void

Exceptions

InvalidStateException

at line 166
IResponse setExpiration(string|null $expire)

Sets the Cache-Control and Expires headers. Pass a time string (e.g. '20 minutes') to enable caching, or null to disable it entirely.

Parameters

string|null $expire

Return Value

IResponse

Exceptions

InvalidStateException

at line 185
bool isSent()

Checks whether HTTP headers have already been sent, making it impossible to modify them.

Return Value

bool

at line 194
string|null getHeader(string $header)

Returns the sent HTTP header, or null if it does not exist. The parameter is case-insensitive.

Parameters

string $header

Return Value

string|null

at line 212
array getHeaders()

Returns all sent HTTP headers as associative array.

Return Value

array

at line 230
IResponse setCookie(string $name, string $value, string|int|DateTimeInterface|null $expire, string|null $path = null, string|null $domain = null, bool $secure = null, bool $httpOnly = true, SameSite $sameSite = SameSite::Lax, bool $partitioned = false)

Sends a cookie.

Parameters

string $name
string $value
string|int|DateTimeInterface|null $expire
string|null $path
string|null $domain
bool $secure
bool $httpOnly
SameSite $sameSite
bool $partitioned

Return Value

IResponse

Exceptions

InvalidStateException

at line 284
void deleteCookie(string $name, string|null $path = null, string|null $domain = null, bool $secure = null)

Deletes a cookie.

Parameters

string $name
string|null $path
string|null $domain
bool $secure

Return Value

void

Exceptions

InvalidStateException

Traits

SmartObject