Documentation

RedirectResponse extends Response
in package

Handle a redirect response

Table of Contents

$CSP  : ContentSecurityPolicy
Content security policy handler
$body  : mixed
Message body
$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.
$headerMap  : array<string|int, mixed>
Holds a map of lower-case header names and their normal-case key as it is in $headers.
$headers  : array<string, Header>
List of all HTTP request headers.
$protocolVersion  : string
Protocol version
$reason  : string
The current reason phrase for this response.
$statusCode  : int
The current status code for this response.
$statusCodes  : array<string|int, mixed>
HTTP status codes
$validProtocolVersions  : array<string|int, mixed>
List of valid protocol versions
__construct()  : mixed
Constructor
appendBody()  : $this
Appends data to the body of the current message.
appendHeader()  : $this
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
back()  : $this
Helper function to return to previous page.
deleteCookie()  : $this
Sets a cookie to be deleted when the response is sent.
download()  : DownloadResponse|null
Force a download.
getBody()  : mixed
Returns the Message's body.
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.
getHeader()  : array<string|int, mixed>|Header|null
Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.
getHeaderLine()  : string
Retrieves a comma-separated string of the values for a single header.
getHeaders()  : array<string, Header>
Returns an array containing all headers.
getJSON()  : mixed|string
Returns the current body, converted to JSON is it isn't already.
getProtocolVersion()  : string
Returns the HTTP Protocol Version.
getReason()  : string
Gets the response response phrase associated with the status code.
getReasonPhrase()  : string
Gets the response reason phrase associated with the status code.
getStatusCode()  : int
Gets the response status code.
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.
hasHeader()  : bool
Determines whether a header exists.
header()  : array<string|int, mixed>|Header|null
Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.
headers()  : array<string, Header>
Returns an array containing all Headers.
noCache()  : Response
Sets the appropriate headers to ensure this response is not cached by the browsers.
populateHeaders()  : void
Populates the $headers array with any headers the getServer knows about.
prependHeader()  : $this
Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)
pretend()  : $this
Turns "pretend" mode on or off to aid in testing.
redirect()  : $this
Perform a redirect to a new URL, in two flavors: header or location.
removeHeader()  : $this
Removes a header from the list of headers we track.
route()  : $this
Sets the URI to redirect to but as a reverse-routed or named route instead of a raw URI.
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.
setBody()  : $this
Sets the body of the current message.
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
setHeader()  : $this
Sets a header and it's value.
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
setProtocolVersion()  : $this
Sets the HTTP protocol version.
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.
to()  : $this
Sets the URI to redirect to and, optionally, the HTTP status code to use.
with()  : $this
Adds a key and message to the session as Flashdata.
withCookies()  : $this|RedirectResponse
Copies any cookies from the global Response instance into this RedirectResponse. Useful when you've just set a cookie but need ensure that's actually sent with the response instead of lost.
withHeaders()  : $this|RedirectResponse
Copies any headers from the global Response instance into this RedirectResponse. Useful when you've just set a header be need to ensure its actually sent with the redirect response.
withInput()  : $this
Specifies that the current $_GET and $_POST arrays should be packaged up with the response.
formatBody()  : mixed
Handles conversion of the of the data into the appropriate format, and sets the correct Content-Type header for our response.
getHeaderName()  : string
Takes a header name in any case, and returns the normal-case version of the header.
sendCookies()  : mixed
Actually sets the cookies.

Properties

$body

Message body

protected mixed $body

$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
deprecated

Use the dedicated Cookie class instead.

$cookieHTTPOnly

Cookie will only be accessible via HTTP(S) (no javascript)

protected bool $cookieHTTPOnly = false
Tags
deprecated

Use the dedicated Cookie class instead.

$cookiePath

Typically will be a forward slash

protected string $cookiePath = '/'
Tags
deprecated

Use the dedicated Cookie class instead.

$cookiePrefix

Set a cookie name prefix if you need to avoid collisions

protected string $cookiePrefix = ''
Tags
deprecated

Use the dedicated Cookie class instead.

$cookies

Stores all cookies that were set in the response.

protected array<string|int, mixed> $cookies = []
Tags
deprecated

Use the dedicated Cookie class instead.

$cookieSameSite

Cookie SameSite setting

protected string $cookieSameSite = CodeIgniterCookieCookie::SAMESITE_LAX
Tags
deprecated

Use the dedicated Cookie class instead.

$cookieSecure

Cookie will only be set if a secure HTTPS connection exists.

protected bool $cookieSecure = false
Tags
deprecated

Use the dedicated Cookie class instead.

$CSPEnabled

Whether Content Security Policy is being enforced.

protected bool $CSPEnabled = false

$headerMap

Holds a map of lower-case header names and their normal-case key as it is in $headers.

protected array<string|int, mixed> $headerMap = []

Used for case-insensitive header access.

$headers

List of all HTTP request headers.

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

$protocolVersion

Protocol version

protected string $protocolVersion

$reason

The current reason phrase for this response.

protected string $reason = ''

If empty string, will use the default provided for the status code.

$statusCode

The current status code for this response.

protected int $statusCode = 200

The status code is a 3-digit integer result code of the server's attempt to understand and satisfy the request.

$statusCodes

HTTP status codes

protected static array<string|int, mixed> $statusCodes = [ // 1xx: Informational 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', // http://www.iana.org/go/rfc2518 103 => 'Early Hints', // http://www.ietf.org/rfc/rfc8297.txt // 2xx: Success 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', // 1.1 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-Status', // http://www.iana.org/go/rfc4918 208 => 'Already Reported', // http://www.iana.org/go/rfc5842 226 => 'IM Used', // 1.1; http://www.ietf.org/rfc/rfc3229.txt // 3xx: Redirection 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', // Formerly 'Moved Temporarily' 303 => 'See Other', // 1.1 304 => 'Not Modified', 305 => 'Use Proxy', // 1.1 306 => 'Switch Proxy', // No longer used 307 => 'Temporary Redirect', // 1.1 308 => 'Permanent Redirect', // 1.1; Experimental; http://www.ietf.org/rfc/rfc7238.txt // 4xx: Client error 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 418 => "I'm a teapot", // April's Fools joke; http://www.ietf.org/rfc/rfc2324.txt // 419 (Authentication Timeout) is a non-standard status code with unknown origin 421 => 'Misdirected Request', // http://www.iana.org/go/rfc7540 Section 9.1.2 422 => 'Unprocessable Entity', // http://www.iana.org/go/rfc4918 423 => 'Locked', // http://www.iana.org/go/rfc4918 424 => 'Failed Dependency', // http://www.iana.org/go/rfc4918 425 => 'Too Early', // https://datatracker.ietf.org/doc/draft-ietf-httpbis-replay/ 426 => 'Upgrade Required', 428 => 'Precondition Required', // 1.1; http://www.ietf.org/rfc/rfc6585.txt 429 => 'Too Many Requests', // 1.1; http://www.ietf.org/rfc/rfc6585.txt 431 => 'Request Header Fields Too Large', // 1.1; http://www.ietf.org/rfc/rfc6585.txt 451 => 'Unavailable For Legal Reasons', // http://tools.ietf.org/html/rfc7725 499 => 'Client Closed Request', // http://lxr.nginx.org/source/src/http/ngx_http_request.h#0133 // 5xx: Server error 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 506 => 'Variant Also Negotiates', // 1.1; http://www.ietf.org/rfc/rfc2295.txt 507 => 'Insufficient Storage', // http://www.iana.org/go/rfc4918 508 => 'Loop Detected', // http://www.iana.org/go/rfc5842 510 => 'Not Extended', // http://www.ietf.org/rfc/rfc2774.txt 511 => 'Network Authentication Required', // http://www.ietf.org/rfc/rfc6585.txt 599 => 'Network Connect Timeout Error', ]

$validProtocolVersions

List of valid protocol versions

protected array<string|int, mixed> $validProtocolVersions = ['1.0', '1.1', '2.0']

Methods

__construct()

Constructor

public __construct(App $config) : mixed
Parameters
$config : App
Tags
todo

Recommend removing reliance on config injection

Return values
mixed

appendBody()

Appends data to the body of the current message.

public appendBody(mixed $data) : $this
Parameters
$data : mixed
Return values
$this

appendHeader()

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

public appendHeader(string $name, string|null $value) : $this
Parameters
$name : string
$value : string|null
Return values
$this

back()

Helper function to return to previous page.

public back([int|null $code = null ][, string $method = 'auto' ]) : $this

Example: return redirect()->back();

Parameters
$code : int|null = null
$method : string = 'auto'
Return values
$this

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

getBody()

Returns the Message's body.

public getBody() : mixed
Return values
mixed

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>

getHeader()

Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.

public getHeader(string $name) : array<string|int, mixed>|Header|null
Parameters
$name : string
Tags
deprecated

Use Message::header() to make room for PSR-7

codeCoverageIgnore
Return values
array<string|int, mixed>|Header|null

getHeaderLine()

Retrieves a comma-separated string of the values for a single header.

public getHeaderLine(string $name) : string

This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.

NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.

Parameters
$name : string
Return values
string

getHeaders()

Returns an array containing all headers.

public getHeaders() : array<string, Header>
Tags
deprecated

Use Message::headers() to make room for PSR-7

codeCoverageIgnore
Return values
array<string, Header>

An array of the request headers

getJSON()

Returns the current body, converted to JSON is it isn't already.

public getJSON() : mixed|string
Tags
throws
InvalidArgumentException

If the body property is not array.

Return values
mixed|string

getProtocolVersion()

Returns the HTTP Protocol Version.

public getProtocolVersion() : string
Return values
string

getReasonPhrase()

Gets the response reason phrase associated with the status code.

public getReasonPhrase() : string

Because a reason phrase is not a required element in a response status line, the reason phrase value MAY be null. Implementations MAY choose to return the default RFC 7231 recommended reason phrase (or those listed in the IANA HTTP Status Code Registry) for the response's status code.

Tags
link
http://tools.ietf.org/html/rfc7231#section-6
link
http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
Return values
string

Reason phrase; must return an empty string if none present.

getStatusCode()

Gets the response status code.

public getStatusCode() : int

The status code is a 3-digit integer result code of the getServer's attempt to understand and satisfy the request.

Return values
int

Status code.

getXML()

Retrieves the current body into XML and returns it.

public getXML() : mixed|string
Tags
throws
InvalidArgumentException

If the body property is not array.

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

hasHeader()

Determines whether a header exists.

public hasHeader(string $name) : bool
Parameters
$name : string
Return values
bool

header()

Returns a single Header object. If multiple headers with the same name exist, then will return an array of header objects.

public header(string $name) : array<string|int, mixed>|Header|null
Parameters
$name : string
Return values
array<string|int, mixed>|Header|null

headers()

Returns an array containing all Headers.

public headers() : array<string, Header>
Return values
array<string, Header>

An array of the Header objects

noCache()

Sets the appropriate headers to ensure this response is not cached by the browsers.

public noCache() : Response
Tags
todo

Recommend researching these directives, might need: 'private', 'no-transform', 'no-store', 'must-revalidate'

see
DownloadResponse::noCache()
Return values
Response

populateHeaders()

Populates the $headers array with any headers the getServer knows about.

public populateHeaders() : void
Return values
void

prependHeader()

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

public prependHeader(string $name, string $value) : $this
Parameters
$name : string
$value : string
Return values
$this

pretend()

Turns "pretend" mode on or off to aid in testing.

public pretend([bool $pretend = true ]) : $this

Note that this is not a part of the interface so should not be relied on outside of internal testing.

Parameters
$pretend : bool = true
Return values
$this

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
throws
HTTPException

For invalid status code.

Return values
$this

removeHeader()

Removes a header from the list of headers we track.

public removeHeader(string $name) : $this
Parameters
$name : string
Return values
$this

route()

Sets the URI to redirect to but as a reverse-routed or named route instead of a raw URI.

public route(string $route[, array<string|int, mixed> $params = [] ][, int $code = 302 ][, string $method = 'auto' ]) : $this
Parameters
$route : string
$params : array<string|int, mixed> = []
$code : int = 302
$method : string = 'auto'
Tags
throws
HTTPException
Return values
$this

setBody()

Sets the body of the current message.

public setBody(mixed $data) : $this
Parameters
$data : mixed
Return values
$this

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

setHeader()

Sets a header and it's value.

public setHeader(string $name, array<string|int, mixed>|null|string $value) : $this
Parameters
$name : string
$value : array<string|int, mixed>|null|string
Return values
$this

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

setProtocolVersion()

Sets the HTTP protocol version.

public setProtocolVersion(string $version) : $this
Parameters
$version : string
Tags
throws
HTTPException

For invalid protocols

Return values
$this

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
see
http://tools.ietf.org/html/rfc7231#section-6
see
http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
throws
HTTPException

For invalid status code arguments.

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

to()

Sets the URI to redirect to and, optionally, the HTTP status code to use.

public to(string $uri[, int|null $code = null ][, string $method = 'auto' ]) : $this

If no code is provided it will be automatically determined.

Parameters
$uri : string

The URI to redirect to

$code : int|null = null

HTTP status code

$method : string = 'auto'
Return values
$this

with()

Adds a key and message to the session as Flashdata.

public with(string $key, string|array<string|int, mixed> $message) : $this
Parameters
$key : string
$message : string|array<string|int, mixed>
Return values
$this

withCookies()

Copies any cookies from the global Response instance into this RedirectResponse. Useful when you've just set a cookie but need ensure that's actually sent with the response instead of lost.

public withCookies() : $this|RedirectResponse
Return values
$this|RedirectResponse

withHeaders()

Copies any headers from the global Response instance into this RedirectResponse. Useful when you've just set a header be need to ensure its actually sent with the redirect response.

public withHeaders() : $this|RedirectResponse
Return values
$this|RedirectResponse

withInput()

Specifies that the current $_GET and $_POST arrays should be packaged up with the response.

public withInput() : $this

It will then be available via the 'old()' helper function.

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
throws
InvalidArgumentException

If the body property is not string or array.

Return values
mixed

getHeaderName()

Takes a header name in any case, and returns the normal-case version of the header.

protected getHeaderName(string $name) : string
Parameters
$name : string
Return values
string

sendCookies()

Actually sets the cookies.

protected sendCookies() : mixed
Return values
mixed

Search results