ResponseTrait
Response Trait
Additional methods to make a PSR-7 Response class compliant with the framework's own ResponseInterface.
Tags
Table of Contents
Properties
- $bodyFormat : string
- Type of format the body is in.
- $cookieStore : CookieStore
- CookieStore instance.
- $CSP : ContentSecurityPolicy
- Content security policy handler
Methods
- deleteCookie() : $this
- Sets a cookie to be deleted when the response is sent.
- download() : DownloadResponse|null
- Force a download.
- getCookie() : array<string, Cookie>|Cookie|null
- Returns the cookie
- getCookies() : array<string, Cookie>
- Returns all cookies currently set.
- getCookieStore() : CookieStore
- Returns the `CookieStore` instance.
- getCSP() : ContentSecurityPolicy
- getJSON() : string|null
- Returns the current body, converted to JSON is it isn't already.
- getXML() : bool|string|null
- Retrieves the current body into XML and returns it.
- hasCookie() : bool
- Checks to see if the Response has a specified cookie or not.
- noCache() : $this
- Sets the appropriate headers to ensure this response is not cached by the browsers.
- redirect() : $this
- Perform a redirect to a new URL, in two flavors: header or location.
- send() : $this
- Sends the output to the browser.
- sendBody() : $this
- Sends the Body of the message to the browser.
- sendHeaders() : $this
- Sends the headers of this HTTP response to the browser.
- setCache() : $this
- A shortcut method that allows the developer to set all of the cache-control headers in one method call.
- setContentType() : $this
- Sets the Content Type header for this response with the mime type and, optionally, the charset.
- setCookie() : $this
- Set a cookie
- setDate() : $this
- Sets the date header
- setJSON() : $this
- Converts the $body into JSON and sets the Content Type header.
- setLastModified() : $this
- Sets the Last-Modified date header.
- setLink() : $this
- Set the Link Header
- setStatusCode() : $this
- Return an instance with the specified status code and, optionally, reason phrase.
- setXML() : $this
- Converts $body into XML, and sets the correct Content-Type.
- formatBody() : false|string
- Handles conversion of the data into the appropriate format, and sets the correct Content-Type header for our response.
- sendCookies() : void
- Actually sets the cookies.
- dispatchCookies() : void
- doSetCookie() : void
- Extracted call to `setcookie()` in order to run unit tests on it.
- doSetRawCookie() : void
- Extracted call to `setrawcookie()` in order to run unit tests on it.
Properties
$bodyFormat
Type of format the body is in.
protected
string
$bodyFormat
= 'html'
Valid: html, json, xml
$cookieStore
CookieStore instance.
protected
CookieStore
$cookieStore
$CSP
Content security policy handler
protected
ContentSecurityPolicy
$CSP
Methods
deleteCookie()
Sets a cookie to be deleted when the response is sent.
public
deleteCookie([string $name = '' ][, string $domain = '' ][, string $path = '/' ][, string $prefix = '' ]) : $this
Parameters
- $name : string = ''
- $domain : string = ''
- $path : string = '/'
- $prefix : string = ''
Return values
$thisdownload()
Force a download.
public
download([string $filename = '' ][, string|null $data = '' ][, bool $setMime = false ]) : DownloadResponse|null
Generates the headers that force a download to happen. And sends the file to the browser.
Parameters
- $filename : string = ''
-
The name you want the downloaded file to be named or the path to the file to send
- $data : string|null = ''
-
The data to be downloaded. Set null if the $filename is the file path
- $setMime : bool = false
-
Whether to try and send the actual MIME type
Return values
DownloadResponse|nullgetCookie()
Returns the cookie
public
getCookie([string|null $name = null ][, string $prefix = '' ]) : array<string, Cookie>|Cookie|null
Parameters
- $name : string|null = null
- $prefix : string = ''
-
Cookie prefix. '': the default prefix
Return values
array<string, Cookie>|Cookie|nullgetCookies()
Returns all cookies currently set.
public
getCookies() : array<string, Cookie>
Return values
array<string, Cookie>getCookieStore()
Returns the `CookieStore` instance.
public
getCookieStore() : CookieStore
Return values
CookieStoregetCSP()
public
getCSP() : ContentSecurityPolicy
Return values
ContentSecurityPolicygetJSON()
Returns the current body, converted to JSON is it isn't already.
public
getJSON() : string|null
Tags
Return values
string|nullgetXML()
Retrieves the current body into XML and returns it.
public
getXML() : bool|string|null
Tags
Return values
bool|string|nullhasCookie()
Checks to see if the Response has a specified cookie or not.
public
hasCookie(string $name[, string|null $value = null ][, string $prefix = '' ]) : bool
Parameters
- $name : string
- $value : string|null = null
- $prefix : string = ''
Return values
boolnoCache()
Sets the appropriate headers to ensure this response is not cached by the browsers.
public
noCache() : $this
Tags
Return values
$thisredirect()
Perform a redirect to a new URL, in two flavors: header or location.
public
redirect(string $uri[, string $method = 'auto' ][, int|null $code = null ]) : $this
Parameters
- $uri : string
-
The URI to redirect to
- $method : string = 'auto'
- $code : int|null = null
-
The type of redirection, defaults to 302
Tags
Return values
$thissend()
Sends the output to the browser.
public
send() : $this
Return values
$thissendBody()
Sends the Body of the message to the browser.
public
sendBody() : $this
Return values
$thissendHeaders()
Sends the headers of this HTTP response to the browser.
public
sendHeaders() : $this
Return values
$thissetCache()
A shortcut method that allows the developer to set all of the cache-control headers in one method call.
public
setCache([array<string|int, mixed> $options = [] ]) : $this
The options array is used to provide the cache-control directives for the header. It might look something like:
$options = [
'max-age' => 300,
's-maxage' => 900
'etag' => 'abcde',
];
Typical options are:
- etag
- last-modified
- max-age
- s-maxage
- private
- public
- must-revalidate
- proxy-revalidate
- no-transform
Parameters
- $options : array<string|int, mixed> = []
Return values
$thissetContentType()
Sets the Content Type header for this response with the mime type and, optionally, the charset.
public
setContentType(string $mime[, string $charset = 'UTF-8' ]) : $this
Parameters
- $mime : string
- $charset : string = 'UTF-8'
Return values
$thissetCookie()
Set a cookie
public
setCookie(array<string|int, mixed>|Cookie|string $name[, string $value = '' ][, int $expire = 0 ][, string $domain = '' ][, string $path = '/' ][, string $prefix = '' ][, bool|null $secure = null ][, bool|null $httponly = null ][, string|null $samesite = null ]) : $this
Accepts an arbitrary number of binds (up to 7) or an associative array in the first parameter containing all the values.
Parameters
- $name : array<string|int, mixed>|Cookie|string
-
Cookie name / array containing binds / Cookie object
- $value : string = ''
-
Cookie value
- $expire : int = 0
-
Cookie expiration time in seconds
- $domain : string = ''
-
Cookie domain (e.g.: '.yourdomain.com')
- $path : string = '/'
-
Cookie path (default: '/')
- $prefix : string = ''
-
Cookie name prefix ('': the default prefix)
- $secure : bool|null = null
-
Whether to only transfer cookies via SSL
- $httponly : bool|null = null
-
Whether only make the cookie accessible via HTTP (no javascript)
- $samesite : string|null = null
Return values
$thissetDate()
Sets the date header
public
setDate(DateTime $date) : $this
Parameters
- $date : DateTime
Return values
$thissetJSON()
Converts the $body into JSON and sets the Content Type header.
public
setJSON(array<string|int, mixed>|object|string $body[, bool $unencoded = false ]) : $this
Parameters
- $body : array<string|int, mixed>|object|string
- $unencoded : bool = false
Return values
$thissetLastModified()
Sets the Last-Modified date header.
public
setLastModified(DateTime|string $date) : $this
$date can be either a string representation of the date or, preferably, an instance of DateTime.
Parameters
- $date : DateTime|string
Return values
$thissetLink()
Set the Link Header
public
setLink(PagerInterface $pager) : $this
Parameters
- $pager : PagerInterface
Tags
Return values
$thissetStatusCode()
Return an instance with the specified status code and, optionally, reason phrase.
public
setStatusCode(int $code[, string $reason = '' ]) : $this
If no reason phrase is specified, will default recommended reason phrase for the response's status code.
Parameters
- $code : int
-
The 3-digit integer result code to set.
- $reason : string = ''
-
The reason phrase to use with the provided status code; if none is provided, will default to the IANA name.
Tags
Return values
$thissetXML()
Converts $body into XML, and sets the correct Content-Type.
public
setXML(array<string|int, mixed>|string $body) : $this
Parameters
- $body : array<string|int, mixed>|string
Return values
$thisformatBody()
Handles conversion of the data into the appropriate format, and sets the correct Content-Type header for our response.
protected
formatBody(array<string|int, mixed>|object|string $body, string $format) : false|string
Parameters
- $body : array<string|int, mixed>|object|string
- $format : string
-
Valid: json, xml
Tags
Return values
false|stringsendCookies()
Actually sets the cookies.
protected
sendCookies() : void
dispatchCookies()
private
dispatchCookies() : void
doSetCookie()
Extracted call to `setcookie()` in order to run unit tests on it.
private
doSetCookie(string $name, string $value, array<string|int, mixed> $options) : void
Parameters
- $name : string
- $value : string
- $options : array<string|int, mixed>
Tags
doSetRawCookie()
Extracted call to `setrawcookie()` in order to run unit tests on it.
private
doSetRawCookie(string $name, string $value, array<string|int, mixed> $options) : void
Parameters
- $name : string
- $value : string
- $options : array<string|int, mixed>