ResponseTrait
Request Trait
Additional methods to make a PSR-7 Response class compliant with the framework's own ResponseInterface.
Tags
Table of Contents
- $CSP : ContentSecurityPolicy
- Content security policy handler
- $statusCodes : array<string|int, mixed>
- $bodyFormat : string
- Type of format the body is in.
- $cookieDomain : string
- Set to .your-domain.com for site-wide cookies
- $cookieHTTPOnly : bool
- Cookie will only be accessible via HTTP(S) (no javascript)
- $cookiePath : string
- Typically will be a forward slash
- $cookiePrefix : string
- Set a cookie name prefix if you need to avoid collisions
- $cookies : array<string|int, mixed>
- Stores all cookies that were set in the response.
- $cookieSameSite : string
- Cookie SameSite setting
- $cookieSecure : bool
- Cookie will only be set if a secure HTTPS connection exists.
- $cookieStore : CookieStore
- CookieStore instance.
- $CSPEnabled : bool
- Whether Content Security Policy is being enforced.
- deleteCookie() : $this
- Sets a cookie to be deleted when the response is sent.
- download() : DownloadResponse|null
- Force a download.
- getCookie() : array<string|int, Cookie>|Cookie|null
- Returns the cookie
- getCookies() : array<string|int, Cookie>
- Returns all cookies currently set.
- getCookieStore() : CookieStore
- Returns the `CookieStore` instance.
- getJSON() : mixed|string
- Returns the current body, converted to JSON is it isn't already.
- getXML() : mixed|string
- Retrieves the current body into XML and returns it.
- hasCookie() : bool
- Checks to see if the Response has a specified cookie or not.
- noCache() : Response
- 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() : Response
- Sends the output to the browser.
- sendBody() : Response
- Sends the Body of the message to the browser.
- sendHeaders() : Response
- Sends the headers of this HTTP request to the browser.
- setCache() : Response
- A shortcut method that allows the developer to set all of the cache-control headers in one method call.
- setContentType() : Response
- Sets the Content Type header for this response with the mime type and, optionally, the charset.
- setCookie() : $this
- Set a cookie
- setDate() : Response
- Sets the date header
- setJSON() : $this
- Converts the $body into JSON and sets the Content Type header.
- setLastModified() : Response
- Sets the Last-Modified date header.
- setLink() : Response
- 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() : mixed
- Handles conversion of the of the data into the appropriate format, and sets the correct Content-Type header for our response.
- sendCookies() : mixed
- Actually sets the cookies.
Properties
$CSP
Content security policy handler
public
ContentSecurityPolicy
$CSP
$statusCodes
public
array<string|int, mixed>
$statusCodes
$bodyFormat
Type of format the body is in.
protected
string
$bodyFormat
= 'html'
Valid: html, json, xml
$cookieDomain
Set to .your-domain.com for site-wide cookies
protected
string
$cookieDomain
= ''
Tags
$cookieHTTPOnly
Cookie will only be accessible via HTTP(S) (no javascript)
protected
bool
$cookieHTTPOnly
= false
Tags
$cookiePath
Typically will be a forward slash
protected
string
$cookiePath
= '/'
Tags
$cookiePrefix
Set a cookie name prefix if you need to avoid collisions
protected
string
$cookiePrefix
= ''
Tags
$cookies
Stores all cookies that were set in the response.
protected
array<string|int, mixed>
$cookies
= []
Tags
$cookieSameSite
Cookie SameSite setting
protected
string
$cookieSameSite
= CodeIgniterCookieCookie::SAMESITE_LAX
Tags
$cookieSecure
Cookie will only be set if a secure HTTPS connection exists.
protected
bool
$cookieSecure
= false
Tags
$cookieStore
CookieStore instance.
protected
CookieStore
$cookieStore
$CSPEnabled
Whether Content Security Policy is being enforced.
protected
bool
$CSPEnabled
= false
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
$this —download()
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 path to the file to send
- $data : string|null = ''
-
The data to be downloaded
- $setMime : bool = false
-
Whether to try and send the actual MIME type
Return values
DownloadResponse|null —getCookie()
Returns the cookie
public
getCookie([string|null $name = null ][, string $prefix = '' ]) : array<string|int, Cookie>|Cookie|null
Parameters
- $name : string|null = null
- $prefix : string = ''
Return values
array<string|int, Cookie>|Cookie|null —getCookies()
Returns all cookies currently set.
public
getCookies() : array<string|int, Cookie>
Return values
array<string|int, Cookie> —getCookieStore()
Returns the `CookieStore` instance.
public
getCookieStore() : CookieStore
Return values
CookieStore —getJSON()
Returns the current body, converted to JSON is it isn't already.
public
getJSON() : mixed|string
Tags
Return values
mixed|string —getXML()
Retrieves the current body into XML and returns it.
public
getXML() : mixed|string
Tags
Return values
mixed|string —hasCookie()
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
bool —noCache()
Sets the appropriate headers to ensure this response is not cached by the browsers.
public
noCache() : Response
Tags
Return values
Response —redirect()
Perform a redirect to a new URL, in two flavors: header or location.
public
redirect(string $uri[, string $method = 'auto' ][, int $code = null ]) : $this
Parameters
- $uri : string
-
The URI to redirect to
- $method : string = 'auto'
- $code : int = null
-
The type of redirection, defaults to 302
Tags
Return values
$this —send()
Sends the output to the browser.
public
send() : Response
Return values
Response —sendBody()
Sends the Body of the message to the browser.
public
sendBody() : Response
Return values
Response —sendHeaders()
Sends the headers of this HTTP request to the browser.
public
sendHeaders() : Response
Return values
Response —setCache()
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 = [] ]) : Response
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
Response —setContentType()
Sets the Content Type header for this response with the mime type and, optionally, the charset.
public
setContentType(string $mime[, string $charset = 'UTF-8' ]) : Response
Parameters
- $mime : string
- $charset : string = 'UTF-8'
Return values
Response —setCookie()
Set a cookie
public
setCookie(string|array<string|int, mixed> $name[, string $value = '' ][, string $expire = '' ][, string $domain = '' ][, string $path = '/' ][, string $prefix = '' ][, bool $secure = false ][, bool $httponly = false ][, 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 : string|array<string|int, mixed>
-
Cookie name or array containing binds
- $value : string = ''
-
Cookie value
- $expire : string = ''
-
Cookie expiration time in seconds
- $domain : string = ''
-
Cookie domain (e.g.: '.yourdomain.com')
- $path : string = '/'
-
Cookie path (default: '/')
- $prefix : string = ''
-
Cookie name prefix
- $secure : bool = false
-
Whether to only transfer cookies via SSL
- $httponly : bool = false
-
Whether only make the cookie accessible via HTTP (no javascript)
- $samesite : string|null = null
Return values
$this —setDate()
Sets the date header
public
setDate(DateTime $date) : Response
Parameters
- $date : DateTime
Return values
Response —setJSON()
Converts the $body into JSON and sets the Content Type header.
public
setJSON(array<string|int, mixed>|string $body[, bool $unencoded = false ]) : $this
Parameters
- $body : array<string|int, mixed>|string
- $unencoded : bool = false
Return values
$this —setLastModified()
Sets the Last-Modified date header.
public
setLastModified(DateTime|string $date) : Response
$date can be either a string representation of the date or, preferably, an instance of DateTime.
Parameters
- $date : DateTime|string
Return values
Response —setLink()
Set the Link Header
public
setLink(PagerInterface $pager) : Response
Parameters
- $pager : PagerInterface
Tags
Return values
Response —setStatusCode()
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
$this —setXML()
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
$this —formatBody()
Handles conversion of the of the data into the appropriate format, and sets the correct Content-Type header for our response.
protected
formatBody(string|array<string|int, mixed> $body, string $format) : mixed
Parameters
- $body : string|array<string|int, mixed>
- $format : string
-
Valid: json, xml
Tags
Return values
mixed —sendCookies()
Actually sets the cookies.
protected
sendCookies() : mixed