Responses
After finishing the processing of the request object, Yii2 then generates a response object, which is sent back to the client. The response contains a myriad of information, such as the HTTP status code, response body, and headers. In Yii2, the response object is implemented by yii\web\Response
, which is represented by the response
application component. In this section, we'll explore how to work with responses.
Setting status codes
In most cases, Yii2 is perfectly capable of setting the appropriate response code back to the end user; however, there may be situations that require us to explicitly define the HTTP response code for our application. To modify the HTTP status code within our application, we simply need to set yii\web\Response::$statusCode
to a valid HTTP status code:
\Yii::$app->response->statusCode = 200;
Web exceptions
By default, Yii2 will return an HTTP 200 status code for any successful request. If we want to adjust the status code without interrupting our flow...