Craft 3 Class Reference

Class craft\web\Response

Inheritance
craft\web\Response » yii\web\Response » yii\base\Response » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Available since version
3.0
Source Code
https://github.com/craftcms/cms/blob/master/src/web/Response.php

Public Properties
Property Type Description Defined By
$acceptMimeType string The MIME type (e.g. application/json) from the request ACCEPT header chosen for this response. yii\web\Response
$acceptParams array The parameters (e.g. `['q' => 1, 'version' => '1. yii\web\Response
$charset string The charset of the text response. yii\web\Response
$content string The response content. yii\web\Response
$data mixed The original response data. yii\web\Response
$exitStatus integer The exit status. yii\base\Response
$format string The response format. yii\web\Response
$formatters array The formatters for converting data into the response content of the specified $format. yii\web\Response
$httpStatuses array List of HTTP status codes and the corresponding texts yii\web\Response
$isSent boolean Whether the response has been sent. yii\web\Response
$statusText string The HTTP status description that comes together with the status code. yii\web\Response
$stream resource, array The stream to be sent. yii\web\Response
$version string The version of the HTTP protocol to use. yii\web\Response
Public Methods
Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
clear() Clears the headers, cookies, content, status code of the response. yii\web\Response
clearOutputBuffers() Removes all existing output buffers. yii\base\Response
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getContentType() Returns the Content-Type header (sans charset=X) that the response will most likely include. craft\web\Response
getCookies() Returns the cookie collection. yii\web\Response
getHeaders() Returns the header collection. yii\web\Response
getIsClientError() yii\web\Response
getIsEmpty() yii\web\Response
getIsForbidden() yii\web\Response
getIsInformational() yii\web\Response
getIsInvalid() yii\web\Response
getIsNotFound() yii\web\Response
getIsOk() yii\web\Response
getIsRedirection() yii\web\Response
getIsServerError() yii\web\Response
getIsSuccessful() yii\web\Response
getStatusCode() yii\web\Response
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
redirect() Redirects the browser to the specified URL. yii\web\Response
refresh() Refreshes the current page. yii\web\Response
send() Sends the response to client. yii\base\Response
sendAndClose() Attempts to closes the connection with the HTTP client, without ending PHP script execution. craft\web\Response
sendContentAsFile() Sends the specified content as a file to the browser. craft\web\Response
sendFile() Sends a file to the browser. craft\web\Response
sendStreamAsFile() Sends the specified stream as a file to the browser. yii\web\Response
setCacheHeaders() Sets headers that will instruct the client to cache this response. craft\web\Response
setDownloadHeaders() Sets a default set of HTTP headers for file downloading purpose. yii\web\Response
setLastModifiedHeader() Sets a Last-Modified header based on a given file path. craft\web\Response
setStatusCode() Sets the response status code. yii\web\Response
setStatusCodeByException() Sets the response status code based on the exception. yii\web\Response
trigger() Triggers an event. yii\base\Component
xSendFile() Sends existing file to a browser as a download using x-sendfile. yii\web\Response
Protected Methods
Method Description Defined By
defaultFormatters() yii\web\Response
getDispositionHeaderValue() Returns Content-Disposition header value that is safe to use with both old and new browsers. yii\web\Response
getHttpRange() Determines the HTTP range given in the request. yii\web\Response
prepare() Prepares for sending the response. craft\web\Response
sendContent() Sends the response content to the client. yii\web\Response
sendCookies() Sends the cookies to the client. yii\web\Response
sendHeaders() Sends the response headers to the client. yii\web\Response
Events
Event Type Description Defined By
EVENT_AFTER_PREPARE \yii\web\ResponseEvent An event that is triggered right after prepare() is called in send(). yii\web\Response
EVENT_AFTER_SEND \yii\web\ResponseEvent An event that is triggered at the end of send(). yii\web\Response
EVENT_BEFORE_SEND \yii\web\ResponseEvent An event that is triggered at the beginning of send(). yii\web\Response
Constants
Constant Value Description Defined By
FORMAT_HTML 'html' yii\web\Response
FORMAT_JSON 'json' yii\web\Response
FORMAT_JSONP 'jsonp' yii\web\Response
FORMAT_RAW 'raw' yii\web\Response
FORMAT_XML 'xml' yii\web\Response

Method Details

getContentType() public method #

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

public string, null getContentType ( )

prepare() protected method #

Prepares for sending the response.

The default implementation will convert $data into $content and set headers accordingly.

protected void prepare ( )
throws yii\base\InvalidConfigException if the formatter for the specified format is invalid or $format is not supported

sendAndClose() public method #

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

This method relies on flush(), 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.

public void sendAndClose ( )
throws Throwable An exception will be thrown if content has already been output.

sendContentAsFile() public method #

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() is called explicitly or implicitly. The latter is done after you return from a controller action.

\yii\web\Response::sendContentAsFile()

public static sendContentAsFile ( $content, $attachmentName, $options = [] )
$content string The content to be sent. The existing $content will be discarded.
$attachmentName string The file name shown to the user.
$options array 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.
return static Self reference
throws yii\web\HttpException

sendFile() public method #

Sends a file to the browser.

Note that this method only prepares the response for file sending. The file is not sent until send() 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()

public static sendFile ( $filePath, $attachmentName null, $options = [] )
$filePath string The path of the file to be sent.
$attachmentName string, null The file name shown to the user. If null, it will be determined from $filePath.
$options array 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.
return static Self reference

setCacheHeaders() public method #

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

public static setCacheHeaders ( )
return static Self reference

setLastModifiedHeader() public method #

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

public static setLastModifiedHeader ( \craft\web\string $path )
$path string The file to read the last modified date from.
return static Self reference