Aplus Framework Docs

Response extends Message
in package
implements ResponseInterface uses ResponseDownload

Class Response.

Tags
see
https://developer.mozilla.org/en-US/docs/Web/HTTP/Messages#HTTP_Responses

Table of Contents

Interfaces

ResponseInterface
Interface ResponseInterface.

Properties

$autoEtag  : bool
$autoEtagHashAlgo  : string
$body  : string
HTTP Message Body.
$cacheSeconds  : int
$cookies  : array<string, Cookie>
HTTP Message Cookies.
$csp  : CSP
$cspReportOnly  : CSP
$debugCollector  : HTTPCollector
$headers  : array<string, string>
HTTP Message Headers.
$headersSent  : bool
$inToString  : bool
$isSent  : bool
$jsonFlags  : int
$method  : string
HTTP Request Method.
$protocol  : string
HTTP Message Protocol.
$replaceHeaders  : bool
$request  : Request
$sendedBody  : string|null
$statusCode  : int
HTTP Response Status Code.
$statusReason  : string
HTTP Response Status Reason.
$url  : URL
HTTP Request URL.

Methods

__construct()  : mixed
Response constructor.
__toString()  : string
appendBody()  : static
Append a string to the body.
appendHeader()  : static
Append a Message header.
getBody()  : string
Get the Response body.
getCacheSeconds()  : int
Get the number of seconds the cache is active.
getCookie()  : Cookie|null
Get a Cookie by name.
getCookies()  : array<string, Cookie>
Get all Cookies.
getCsp()  : CSP|null
Get the Content-Security-Policy instance or null.
getCspReportOnly()  : CSP|null
Get the Content-Security-Policy-Report-Only instance or null.
getHeader()  : string|null
getHeaderLine()  : string|null
getHeaderLines()  : array<int, string>
getHeaders()  : array<string, string>
getJsonFlags()  : int
Get JSON flags.
getProtocol()  : string
Get the HTTP protocol.
getRequest()  : Request
getStartLine()  : string
Get the Message Start-Line.
getStatus()  : string
Get the status line without the protocol part.
getStatusCode()  : int
Get the status code.
getStatusReason()  : string
Get the status reason.
hasCookie()  : bool
Say if the Message has a Cookie.
hasCsp()  : bool
Tells if the Content-Security-Policy instance has been set.
hasCspReportOnly()  : bool
Tells if the Content-Security-Policy-Report-Only instance has been set.
hasDownload()  : bool
Tell if Response has a downloadable file.
hasHeader()  : bool
isAutoEtag()  : bool
isReplacingHeaders()  : bool
Tells if headers are being replaced.
isSent()  : bool
Say if the response was sent.
isStatusCode()  : bool
parseQualityValues()  : array<string, float>
prependBody()  : static
Prepend a string to the body.
redirect()  : static
Sets the HTTP Redirect Response with data accessible in the next HTTP Request.
removeCookie()  : static
Remove a Cookie by name.
removeCookies()  : static
Remove many Cookies by names.
removeCsp()  : static
Remove the Content-Security-Policy instance.
removeCspReportOnly()  : static
Remove the Content-Security-Policy-Report-Only instance.
removeHeader()  : static
Remove a header by name.
removeHeaders()  : static
Remove all headers.
removeHeadersByNames()  : static
Remove headers by names.
send()  : void
Send the Response headers, cookies and body to the output.
setAutoEtag()  : static
Enable or disable the capability of auto-add the ETag header and negotiate the response with it.
setBody()  : static
Set the Response body.
setCache()  : static
Set the Cache-Control header.
setContentEncoding()  : static
Set the Content-Encoding header.
setContentLanguage()  : static
Set the Content-Language header.
setContentLength()  : static
Set the Content-Length header.
setContentType()  : static
Set the Content-Type header.
setCookie()  : static
Set a new Cookie.
setCookies()  : static
Set a list of Cookies.
setCsp()  : static
Set the Content-Security-Policy instance.
setCspReportOnly()  : static
Set the Content-Security-Policy-Report-Only instance.
setDate()  : static
Set the Date header.
setDebugCollector()  : static
setDownload()  : static
Sets a file to download/stream.
setEtag()  : static
Set the ETag header.
setExpires()  : static
Set the Expires header.
setHeader()  : static
Set a Message header.
setHeaders()  : static
Set a list of headers.
setJson()  : static
Set Response body and Content-Type as JSON.
setJsonFlags()  : static
Set JSON flags.
setLastModified()  : static
Set the Last-Modified header.
setNoCache()  : static
Clear the browser cache.
setNotModified()  : static
Set the status line as "Not Modified".
setReplaceHeaders()  : static
The replace parameter indicates whether the header should replace a previous similar header, or add a next header of the same type.
setStatus()  : static
Set the status part in the start-line.
setStatusCode()  : static
Set the status code.
setStatusReason()  : static
Set a custom status reason.
getMethod()  : string
Gets the HTTP Request Method.
getUrl()  : URL
Gets the requested URL.
isMethod()  : bool
negotiateContentType()  : void
Negotiates the Content-Type header, setting the MIME type "text/html" if the response body is not empty.
negotiateCsp()  : void
Set the Content-Security-Policy and Content-Security-Policy-Report-Only headers if the CSP classes are set and the response has not downloads.
negotiateEtag()  : void
Set the ETag header, based on the Response body, and start the negotiation.
parseContentType()  : string|null
sendAll()  : void
sendBody()  : void
sendCookies()  : void
sendDownload()  : void
sendHeaders()  : void
Send the HTTP headers to the output.
setMethod()  : static
Set the request method.
setProtocol()  : static
Set the HTTP protocol.
setUrl()  : static
Set the Message URL.

Properties

$autoEtag

protected bool $autoEtag = false

$autoEtagHashAlgo

protected string $autoEtagHashAlgo = 'xxh3'

$body

HTTP Message Body.

protected string $body

$cacheSeconds

protected int $cacheSeconds = 0

$cookies

HTTP Message Cookies.

protected array<string, Cookie> $cookies = []

$headers

HTTP Message Headers.

protected array<string, string> $headers = []

$headersSent

protected bool $headersSent = false

$inToString

protected bool $inToString = false

$jsonFlags

protected int $jsonFlags = \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE

$method

HTTP Request Method.

protected string $method

$protocol

HTTP Message Protocol.

protected string $protocol = \Framework\HTTP\Protocol::HTTP_1_1

$replaceHeaders

protected bool $replaceHeaders = false

$sendedBody

protected string|null $sendedBody = null

$statusCode

HTTP Response Status Code.

protected int $statusCode = \Framework\HTTP\Status::OK

$statusReason

HTTP Response Status Reason.

protected string $statusReason = 'OK'

Methods

__construct()

Response constructor.

public __construct(Request $request) : mixed
Parameters
$request : Request

__toString()

public __toString() : string
Attributes
#[Override]
Return values
string

appendBody()

Append a string to the body.

public appendBody(string $content) : static
Parameters
$content : string
Return values
static

appendHeader()

Append a Message header.

public appendHeader(string $name, string $value) : static

Used to set repeated header field names.

Parameters
$name : string
$value : string
Attributes
#[Override]
Return values
static

getBody()

Get the Response body.

public getBody() : string
Attributes
#[Override]
Return values
string

getCacheSeconds()

Get the number of seconds the cache is active.

public getCacheSeconds() : int
Attributes
#[Pure]
Return values
int

getCookie()

Get a Cookie by name.

public getCookie(string $name) : Cookie|null
Parameters
$name : string
Attributes
#[Pure]
Return values
Cookie|null

getCookies()

Get all Cookies.

public getCookies() : array<string, Cookie>
Attributes
#[Pure]
Return values
array<string, Cookie>

getCsp()

Get the Content-Security-Policy instance or null.

public getCsp() : CSP|null
Return values
CSP|null

getCspReportOnly()

Get the Content-Security-Policy-Report-Only instance or null.

public getCspReportOnly() : CSP|null
Return values
CSP|null

getHeader()

public getHeader(string $name) : string|null
Parameters
$name : string
Attributes
#[Pure]
Return values
string|null

getHeaderLine()

public getHeaderLine(string $name) : string|null
Parameters
$name : string
Attributes
#[Pure]
Return values
string|null

getHeaderLines()

public getHeaderLines() : array<int, string>
Attributes
#[Pure]
Return values
array<int, string>

getHeaders()

public getHeaders() : array<string, string>
Attributes
#[Pure]
Return values
array<string, string>

getJsonFlags()

Get JSON flags.

public getJsonFlags() : int
Tags
since
5.6
Return values
int

getProtocol()

Get the HTTP protocol.

public getProtocol() : string
Attributes
#[Pure]
Return values
string

getStartLine()

Get the Message Start-Line.

public getStartLine() : string
Tags
throws
BadMethodCallException

if $this is not an instance of RequestInterface or ResponseInterface

Return values
string

getStatusCode()

Get the status code.

public getStatusCode() : int
Attributes
#[Override]
#[Pure]
Return values
int

getStatusReason()

Get the status reason.

public getStatusReason() : string
Attributes
#[Pure]
Return values
string

hasCookie()

Say if the Message has a Cookie.

public hasCookie(string $name) : bool
Parameters
$name : string

Cookie name

Attributes
#[Pure]
Return values
bool

hasCsp()

Tells if the Content-Security-Policy instance has been set.

public hasCsp() : bool
Return values
bool

hasCspReportOnly()

Tells if the Content-Security-Policy-Report-Only instance has been set.

public hasCspReportOnly() : bool
Return values
bool

hasDownload()

Tell if Response has a downloadable file.

public hasDownload() : bool
Attributes
#[Pure]
Return values
bool

hasHeader()

public hasHeader(string $name[, string|null $value = null ]) : bool
Parameters
$name : string
$value : string|null = null
Attributes
#[Pure]
Return values
bool

isAutoEtag()

public isAutoEtag() : bool
Return values
bool

isSent()

Say if the response was sent.

public isSent() : bool
Attributes
#[Pure]
Return values
bool

isStatusCode()

public isStatusCode(int $code) : bool
Parameters
$code : int
Tags
throws
InvalidArgumentException

if status code is invalid

Attributes
#[Override]
Return values
bool

prependBody()

Prepend a string to the body.

public prependBody(string $content) : static
Parameters
$content : string
Return values
static

redirect()

Sets the HTTP Redirect Response with data accessible in the next HTTP Request.

public redirect(string $location[, array<string|int, mixed> $data = [] ][, int|null $code = null ]) : static
Parameters
$location : string

Location Header value

$data : array<string|int, mixed> = []

Session data available on next Request

$code : int|null = null

HTTP Redirect status code. Leave null to determine based on the current HTTP method.

Tags
see
http://en.wikipedia.org/wiki/Post/Redirect/Get
Request::getRedirectData()
throws
InvalidArgumentException

for invalid Redirection code

LogicException

if PHP Session is not active to set redirect data

Return values
static

removeCookie()

Remove a Cookie by name.

public removeCookie(string $name) : static
Parameters
$name : string
Attributes
#[Override]
Return values
static

removeCookies()

Remove many Cookies by names.

public removeCookies(array<string|int, mixed> $names) : static
Parameters
$names : array<string|int, mixed>
Attributes
#[Override]
Return values
static

removeCsp()

Remove the Content-Security-Policy instance.

public removeCsp() : static
Return values
static

removeCspReportOnly()

Remove the Content-Security-Policy-Report-Only instance.

public removeCspReportOnly() : static
Return values
static

removeHeader()

Remove a header by name.

public removeHeader(string $name) : static

NOTE: This method does not remove headers set by PHP, such as with sessions or the header() function. To do this type of removal use the header_remove() function.

Parameters
$name : string
Tags
see
https://www.php.net/manual/en/function.header-remove.php
Attributes
#[Override]
Return values
static

removeHeaders()

Remove all headers.

public removeHeaders() : static
Attributes
#[Override]
Return values
static

removeHeadersByNames()

Remove headers by names.

public removeHeadersByNames(array<string|int, string> $names) : static
Parameters
$names : array<string|int, string>
Tags
see
Response::removeHeader()
Attributes
#[Override]
Return values
static

send()

Send the Response headers, cookies and body to the output.

public send() : void
Tags
throws
LogicException

if Response is already sent

setAutoEtag()

Enable or disable the capability of auto-add the ETag header and negotiate the response with it.

public setAutoEtag([bool $active = true ][, string|null $hashAlgo = null ]) : static
Parameters
$active : bool = true
$hashAlgo : string|null = null
Tags
see
Response::negotiateEtag()
Return values
static

setBody()

Set the Response body.

public setBody(string $body) : static
Parameters
$body : string
Attributes
#[Override]
Return values
static

setCookie()

Set a new Cookie.

public setCookie(Cookie $cookie) : static
Parameters
$cookie : Cookie
Attributes
#[Override]
Return values
static

setCookies()

Set a list of Cookies.

public setCookies(array<string|int, mixed> $cookies) : static
Parameters
$cookies : array<string|int, mixed>
Attributes
#[Override]
Return values
static

setCsp()

Set the Content-Security-Policy instance.

public setCsp(CSP $csp) : static
Parameters
$csp : CSP
Return values
static

setCspReportOnly()

Set the Content-Security-Policy-Report-Only instance.

public setCspReportOnly(CSP $csp) : static
Parameters
$csp : CSP
Return values
static

setDownload()

Sets a file to download/stream.

public setDownload(string $filepath[, bool $inline = false ][, bool $acceptRanges = true ][, int $delay = 0 ][, int $readLength = 1024 ][, string|null $filename = null ]) : static
Parameters
$filepath : string
$inline : bool = false

Set Content-Disposition header as "inline". Browsers load the file in the window. Set true to allow video or audio streams

$acceptRanges : bool = true

Set Accept-Ranges header to "bytes". Allow partial downloads, media players to move the time position forward and back and download managers to continue/download multi-parts

$delay : int = 0

Delay between flushs in microseconds

$readLength : int = 1024

Bytes read by flush

$filename : string|null = null

A custom filename

Tags
throws
InvalidArgumentException

If invalid file path

RuntimeException

If can not get the file size or modification time

Return values
static

setHeader()

Set a Message header.

public setHeader(string $name, string $value) : static
Parameters
$name : string
$value : string
Attributes
#[Override]
Return values
static

setHeaders()

Set a list of headers.

public setHeaders(array<string|int, mixed> $headers) : static
Parameters
$headers : array<string|int, mixed>
Attributes
#[Override]
Return values
static

setJson()

Set Response body and Content-Type as JSON.

public setJson(mixed $data[, int|null $flags = null ][, int<1, max> $depth = 512 ]) : static
Parameters
$data : mixed

The data being encoded. Can be any type except a resource.

$flags : int|null = null

[optional]

Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_AMP, JSON_HEX_APOS, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_THROW_ON_ERROR. JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,

Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE when null. JSON_THROW_ON_ERROR is enforced by default.

$depth : int<1, max> = 512

Set the maximum depth. Must be greater than zero.

Tags
see
https://www.php.net/manual/en/function.json-encode.php
https://www.php.net/manual/en/json.constants.php
throws
JsonException

if json_encode() fails

Return values
static

setJsonFlags()

Set JSON flags.

public setJsonFlags(int $flags) : static
Parameters
$flags : int
Tags
since
5.6
Return values
static

setNoCache()

Clear the browser cache.

public setNoCache() : static
Return values
static

setNotModified()

Set the status line as "Not Modified".

public setNotModified() : static
Return values
static

setReplaceHeaders()

The replace parameter indicates whether the header should replace a previous similar header, or add a next header of the same type.

public setReplaceHeaders([bool $replace = true ]) : static

By default, it will replace, but if you pass in false as the first argument you can force multiple headers of the same type.

Parameters
$replace : bool = true
Tags
since
6.5
see
Response::sendHeaders()
Return values
static

setStatus()

Set the status part in the start-line.

public setStatus(int $code[, string|null $reason = null ]) : static
Parameters
$code : int
$reason : string|null = null
Tags
throws
InvalidArgumentException

if status code is invalid

LogicException

is status code is unknown and a reason is not set

Return values
static

setStatusCode()

Set the status code.

public setStatusCode(int $code) : static
Parameters
$code : int
Tags
throws
InvalidArgumentException

if status code is invalid

Attributes
#[Override]
Return values
static

setStatusReason()

Set a custom status reason.

public setStatusReason(string $reason) : static
Parameters
$reason : string
Return values
static

getMethod()

Gets the HTTP Request Method.

protected getMethod() : string
Attributes
#[Pure]
Return values
string —

$method One of: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, or TRACE

getUrl()

Gets the requested URL.

protected getUrl() : URL
Attributes
#[Pure]
Return values
URL

isMethod()

protected isMethod(string $method) : bool
Parameters
$method : string
Tags
throws
InvalidArgumentException

for invalid method

Return values
bool

negotiateContentType()

Negotiates the Content-Type header, setting the MIME type "text/html" if the response body is not empty.

protected negotiateContentType() : void

If the response body is empty, it removes the Content-Type header, and it will not appear to the client from the request.

This prevents the Content-Type from appearing without it being needed in, for example, REST API responses.

Tags
see
https://stackoverflow.com/a/21029402/6027968

negotiateCsp()

Set the Content-Security-Policy and Content-Security-Policy-Report-Only headers if the CSP classes are set and the response has not downloads.

protected negotiateCsp() : void

negotiateEtag()

Set the ETag header, based on the Response body, and start the negotiation.

protected negotiateEtag() : void
  • Empty the body and set a status 304 (Not Modified) if the Request If-None-Match header has the same value of the generated ETag, on GET and HEAD requests.

  • Empty the body and set a status 412 (Precondition Failed) if the Request If-Match header is set and has not the same value of the generated ETag, on non-GET or non-HEAD requests.

Tags
see
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag

parseContentType()

protected parseContentType() : string|null
Attributes
#[Pure]
Return values
string|null

sendCookies()

protected sendCookies() : void

sendHeaders()

Send the HTTP headers to the output.

protected sendHeaders() : void
Tags
throws
LogicException

if headers are already sent

setMethod()

Set the request method.

protected setMethod(string $method) : static
Parameters
$method : string

One of: CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, or TRACE

Tags
throws
InvalidArgumentException

for invalid method

Return values
static

setProtocol()

Set the HTTP protocol.

protected setProtocol(string $protocol) : static
Parameters
$protocol : string

HTTP/1.1, HTTP/2, etc

Return values
static

setUrl()

Set the Message URL.

protected setUrl(URL|string $url) : static
Parameters
$url : URL|string
Return values
static

        
On this page

Search results