Response

Type
Class
Namespace
craft\web
Inherits
craft\web\Response » yii\web\Response (opens new window) » yii\base\Response (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
3.0.0

View source (opens new window)

# Public Properties

Property Description
acceptMimeType (opens new window) string (opens new window) – The MIME type (e.g. application/json) from the request ACCEPT header chosen for this response.
acceptParams (opens new window) array (opens new window) – The parameters (e.g. `['q' => 1, 'version' => '1.
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
charset (opens new window) string (opens new window) – The charset of the text response.
content (opens new window) string (opens new window) – The response content.
contentType string (opens new window), null (opens new window)
cookies (opens new window) yii\web\CookieCollection (opens new window) – The cookie collection.
data (opens new window) mixed – The original response data.
downloadHeaders (opens new window) string (opens new window) – The attachment file name
exitStatus (opens new window) integer (opens new window) – The exit status.
format (opens new window) string (opens new window) – The response format.
formatters (opens new window) array (opens new window) – The formatters for converting data into the response content of the specified format (opens new window).
headers (opens new window) yii\web\HeaderCollection (opens new window) – The header collection
httpStatuses (opens new window) array (opens new window) – List of HTTP status codes and the corresponding texts
isClientError (opens new window) boolean (opens new window) – Whether this response indicates a client error
isEmpty (opens new window) boolean (opens new window) – Whether this response is empty
isForbidden (opens new window) boolean (opens new window) – Whether this response indicates the current request is forbidden
isInformational (opens new window) boolean (opens new window) – Whether this response is informational
isInvalid (opens new window) boolean (opens new window) – Whether this response has a valid statusCode (opens new window).
isNotFound (opens new window) boolean (opens new window) – Whether this response indicates the currently requested resource is not found
isOk (opens new window) boolean (opens new window) – Whether this response is OK
isRedirection (opens new window) boolean (opens new window) – Whether this response is a redirection
isSent (opens new window) boolean (opens new window) – Whether the response has been sent.
isServerError (opens new window) boolean (opens new window) – Whether this response indicates a server error
isSuccessful (opens new window) boolean (opens new window) – Whether this response is successful
lastModifiedHeader string (opens new window) – The file to read the last modified date from.
rawCookies yii\web\CookieCollection (opens new window) – The cookie collection.
statusCode (opens new window) integer (opens new window) – The HTTP status code to send with the response.
statusCodeByException (opens new window) Exception (opens new window), Error (opens new window) – The exception object.
statusText (opens new window) string (opens new window) – The HTTP status description that comes together with the status code.
stream (opens new window) resource (opens new window), array (opens new window) – The stream to be sent.
version (opens new window) string (opens new window) – The version of the HTTP protocol to use.

# contentType

Type
string (opens new window), null (opens new window)
Default value
null
Access
Read-only

View source (opens new window)

# lastModifiedHeader

Type
string (opens new window)
Default value
null
Access
Write-only

The file to read the last modified date from.

View source (opens new window)

# rawCookies

Type
yii\web\CookieCollection (opens new window)
Default value
null
Access
Read-only
Since
3.5.0

The cookie collection.

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
clear() (opens new window) Clears the headers, cookies, content, status code of the response.
clearOutputBuffers() (opens new window) Removes all existing output buffers.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getContentType() Returns the Content-Type header (sans charset=X) that the response will most likely include.
getCookies() (opens new window) Returns the cookie collection.
getHeaders() (opens new window) Returns the header collection.
getIsClientError() (opens new window)
getIsEmpty() (opens new window)
getIsForbidden() (opens new window)
getIsInformational() (opens new window)
getIsInvalid() (opens new window)
getIsNotFound() (opens new window)
getIsOk() (opens new window)
getIsRedirection() (opens new window)
getIsServerError() (opens new window)
getIsSuccessful() (opens new window)
getRawCookies() Returns the “raw” cookie collection.
getStatusCode() (opens new window)
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() (opens new window) Initializes the object.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
redirect() (opens new window) Redirects the browser to the specified URL.
refresh() (opens new window) Refreshes the current page.
send() (opens new window) Sends the response to client.
sendAndClose() Attempts to closes the connection with the HTTP client, without ending PHP script execution.
sendContentAsFile() Sends the specified content as a file to the browser.
sendFile() Sends a file to the browser.
sendStreamAsFile() (opens new window) Sends the specified stream as a file to the browser.
setCacheHeaders() Sets headers that will instruct the client to cache this response.
setDownloadHeaders() (opens new window) Sets a default set of HTTP headers for file downloading purpose.
setLastModifiedHeader() Sets a Last-Modified header based on a given file path.
setNoCacheHeaders() Sets headers that will instruct the client to not cache this response.
setStatusCode() (opens new window) Sets the response status code.
setStatusCodeByException() (opens new window) Sets the response status code based on the exception.
trigger() (opens new window) Triggers an event.
xSendFile() (opens new window) Sends existing file to a browser as a download using x-sendfile.

# getContentType()

Returns the Content-Type header (sans charset=X) that the response will most likely include.

View source (opens new window)

Returns

string (opens new window), null (opens new window)

# getRawCookies()

Since
3.5.0

Returns the “raw” cookie collection.

Works similar to getCookies() (opens new window), but these cookies won’t go through validation, and their values won’t be hashed.

View source (opens new window)

Returns

yii\web\CookieCollection (opens new window) – The cookie collection.

# sendAndClose()

Attempts to closes the connection with the HTTP client, without ending PHP script execution.

This method relies on flush() (opens new window), which may not actually work if mod_deflate or mod_gzip is installed, or if this is a Win32 server.

See also http://stackoverflow.com/a/141026 View source (opens new window)

Throws

# sendContentAsFile()

Sends the specified content as a file to the browser.

Note that this method only prepares the response for file sending. The file is not sent until send() (opens new window) is called explicitly or implicitly. The latter is done after you return from a controller action.

\yii\web\Response::sendContentAsFile()

View source (opens new window)

Arguments

  • $content (string (opens new window)) – The content to be sent. The existing content (opens new window) will be discarded.

  • $attachmentName (string (opens new window)) – The file name shown to the user.

  • $options (array (opens new window)) – Additional options for sending the file. The following options are supported:

  • mimeType: the MIME type of the content. Defaults to 'application/octet-stream'.

  • inline: boolean, whether the browser should open the file within the browser window. Defaults to false, meaning a download dialog will pop up.

Returns

static – Self reference

Throws

# sendFile()

Sends a file to the browser.

Note that this method only prepares the response for file sending. The file is not sent until send() (opens new window) is called explicitly or implicitly. The latter is done after you return from a controller action.

The following is an example implementation of a controller action that allows requesting files from a directory that is not accessible from web:

public function actionFile($filename)
{
    $storagePath = Yii::getAlias('@app/files');

    // check filename for allowed chars (do not allow ../ to avoid security issue: downloading arbitrary files)
    if (!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $filename) || !is_file("$storagePath/$filename")) {
        throw new \yii\web\NotFoundHttpException('The file does not exists.');
    }
    return Yii::$app->response->sendFile("$storagePath/$filename", $filename);
}

\yii\web\Response::sendFile()

View source (opens new window)

Arguments

  • $filePath (string (opens new window)) – The path of the file to be sent.

  • $attachmentName (string (opens new window), null (opens new window)) – The file name shown to the user. If null, it will be determined from $filePath.

  • $options (array (opens new window)) – Additional options for sending the file. The following options are supported:

  • mimeType: the MIME type of the content. If not set, it will be guessed based on $filePath

  • inline: boolean, whether the browser should open the file within the browser window. Defaults to false, meaning a download dialog will pop up.

Returns

static – Self reference

# setCacheHeaders()

Sets headers that will instruct the client to cache this response.

View source (opens new window)

Returns

static – Self reference

# setLastModifiedHeader()

Sets a Last-Modified header based on a given file path.

View source (opens new window)

Arguments

Returns

static – Self reference

# setNoCacheHeaders()

Since
3.5.0

Sets headers that will instruct the client to not cache this response.

View source (opens new window)

Returns

static – Self reference

# Protected Methods

Method Description
defaultFormatters()
getDispositionHeaderValue() (opens new window) Returns Content-Disposition header value that is safe to use with both old and new browsers.
getHttpRange() (opens new window) Determines the HTTP range given in the request.
prepare() Prepares for sending the response.
sendContent() (opens new window) Sends the response content to the client.
sendCookies() Sends the cookies to the client.
sendHeaders() (opens new window) Sends the response headers to the client.

# defaultFormatters()

Since
3.4.0

View source (opens new window)

Returns

array (opens new window) – The formatters that are supported by default

# prepare()

Prepares for sending the response.

The default implementation will convert data (opens new window) into content (opens new window) and set headers accordingly.

View source (opens new window)

Throws

# sendCookies()

Since
3.5.0

Sends the cookies to the client.

View source (opens new window)

# Constants

Constant Description
FORMAT_CSV
FORMAT_HTML
FORMAT_JSON
FORMAT_JSONP
FORMAT_RAW
FORMAT_XML