When an application finishes handling a request, it generates a response object
and sends it to the end user. The response object contains information such as the HTTP status code, HTTP headers and body.
The ultimate goal of Web application development is essentially to build such response objects upon various requests.

In most cases you should mainly deal with the responseapplication component
which is an instance of yii\web\Response, by default. However, Yii also allows you to create your own response
objects and send them to end users as we will explain in the following.

In this section, we will describe how to compose and send responses to end users.

One of the first things you would do when building a response is to state whether the request is successfully handled.
This is done by setting the yii\web\Response::$statusCode property which can take one of the valid
HTTP status codes. For example, to indicate the request
is successfully handled, you may set the status code to be 200, like the following:

Yii::$app->response->statusCode = 200;

However, in most cases you do not need to explicitly set the status code. This is because the default value
of yii\web\Response::$statusCode is 200. And if you want to indicate the request is unsuccessful, you may
throw an appropriate HTTP exception like the following:

thrownew \yii\web\NotFoundHttpException;

When the error handler catches an exception, it will extract the status code
from the exception and assign it to the response. For the yii\web\NotFoundHttpException above, it is
associated with the HTTP status 404. The following HTTP exceptions are predefined in Yii:

Most responses should have a body which gives the content that you want to show to end users.

If you already have a formatted body string, you may assign it to the yii\web\Response::$content property
of the response. For example,

Yii::$app->response->content = 'hello world!';

If your data needs to be formatted before sending it to end users, you should set both of the
format and data properties. The format
property specifies in which format the data should be formatted. For example,

RAW: use this format if you want to send the response directly without applying any formatting.

While the response body can be set explicitly as shown above, in most cases you may set it implicitly by the return value
of action methods. A common use case is like the following:

publicfunctionactionIndex(){
return$this->render('index');
}

The index action above returns the rendering result of the index view. The return value will be taken
by the response component, formatted and then sent to end users.

Because by default the response format is HTML, you should only return a string
in an action method. If you want to use a different response format, you should set it first before returning the data.
For example,

As aforementioned, besides using the default response application component, you can also create your own
response objects and send them to end users. You can do so by returning such object in an action method, like the following,

Note: If you are creating your own response objects, you will not be able to take advantage of the configurations
that you set for the response component in the application configuration. You can, however, use
dependency injection to apply a common configuration to your new response objects.

Browser redirection relies on sending a Location HTTP header. Because this feature is commonly used, Yii provides
some special support for it.

You can redirect the user browser to a URL by calling the yii\web\Response::redirect() method. The method
sets the appropriate Location header with the given URL and returns the response object itself. In an action method,
you can call its shortcut version yii\web\Controller::redirect(). For example,

In the above code, the action method returns the result of the redirect() method. As explained before, the response
object returned by an action method will be used as the response sending to end users.

Info: By default, the yii\web\Response::redirect() method sets the response status code to be 302 which instructs
the browser that the resource being requested is temporarily located in a different URI. You can pass in a status
code 301 to tell the browser that the resource has been permanently relocated.

When the current request is an AJAX request, sending a Location header will not automatically cause the browser
to redirect. To solve this problem, the yii\web\Response::redirect() method sets an X-Redirect header with
the redirection URL as its value. On the client-side, you may write JavaScript code to read this header value and
redirect the browser accordingly.

Info: Yii comes with a yii.js JavaScript file which provides a set of commonly used JavaScript utilities,
including browser redirection based on the X-Redirect header. Therefore, if you are using this JavaScript file
(by registering the yii\web\YiiAsset asset bundle), you do not need to write anything to support AJAX redirection.
More information about yii.js can be found in the Client Scripts Section.

Like browser redirection, file sending is another feature that relies on specific HTTP headers. Yii provides
a set of methods to support various file sending needs. They all have built-in support for the HTTP range header.

These methods have the same method signature with the response object as the return value. If the file
to be sent is very big, you should consider using yii\web\Response::sendStreamAsFile() because it is more
memory efficient. The following example shows how to send a file in a controller action:

If you are calling the file sending method in places other than an action method, you should also call
the yii\web\Response::send() method afterwards to ensure no extra content will be appended to the response.

\Yii::$app->response->sendFile('path/to/file.txt')->send();

Some Web servers have a special file sending support called X-Sendfile. The idea is to redirect the
request for a file to the Web server which will directly serve the file. As a result, the Web application
can terminate earlier while the Web server is sending the file. To use this feature, you may call
the yii\web\Response::xSendFile(). The following list summarizes how to enable the X-Sendfile feature
for some popular Web servers:

The content in a response is not sent to the user until the yii\web\Response::send() method is called.
By default, this method will be called automatically at the end of yii\base\Application::run(). You can, however,
explicitly call this method to force sending out the response immediately.