English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Responses
  • 时间:2024-12-22

Yii - Responses


Previous Page Next Page  

When a web apppcation handles a request, it generates a response object, which contains HTTP headers, body, and HTTP status code. In most cases, you will use the response apppcation component. By default, it is an instance of yiiwebResponse.

To manage response HTTP status codes, use the yiiwebResponse::$statusCode property. The default value of yiiwebResponse::$statusCode is 200.

Step 1 − Add a function named actionTestResponse to the SiteController.

pubpc function actionTestResponse() {
   Yii::$app→response->statusCode = 201;
}

Step 2 − If you point your web browser at http://localhost:8080/index.php?r=site/testresponse, you should notice the 201 Created response HTTP status.

Created response HTTP status

If you want to indicate that the request is unsuccessful, you may throw one of the predefined HTTP exceptions −

    yiiwebBadRequestHttpException − status code 400.

    yiiwebUnauthorizedHttpException − status code 401.

    yiiwebForbiddenHttpException − status code 403.

    yiiwebNotFoundHttpException − status code 404.

    yiiwebMethodNotAllowedHttpException − status code 405.

    yiiwebNotAcceptableHttpException − status code 406.

    yiiwebConfpctHttpException − status code 409.

    yiiwebGoneHttpException − status code 410.

    yiiwebUnsupportedMediaTypeHttpException − status code 415.

    yiiwebTooManyRequestsHttpException − status code 429.

    yiiwebServerErrorHttpException − status code 500.

Step 3 − Modify the actionTestResponse function as shown in the following code.

pubpc function actionTestResponse() {
   throw new yiiwebGoneHttpException;
}

Step 4 − Type http://localhost:8080/index.php?r=site/test-response in the address bar of the web browser, you can see the 410 Gone response HTTP status as shown in the following image.

Gone Response HTTP Status

Step 5 − You can send HTTP headers by modifying the headers property of the response component. To add a new header to a response, modify the actionTestResponse function as given in the following code.

pubpc function actionTestResponse() {
   Yii::$app->response->headers->add( Pragma ,  no-cache );
}

Step 6 − Go to http://localhost:8080/index.php?r=site/test-response, you will see our Pragma header.

Pragma header

Yii supports the following response formats −

    HTML − implemented by yiiwebHtmlResponseFormatter.

    XML − implemented by yiiwebXmlResponseFormatter.

    JSON − implemented by yiiwebJsonResponseFormatter.

    JSONP − implemented by yiiwebJsonResponseFormatter.

    RAW − the response without any formatting.

Step 7 − To respond in the JSON format, modify the actionTestResponse function.

pubpc function actionTestResponse() {
   Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
   return [
       id  =>  1 ,
       name  =>  Ivan ,
       age  => 24,
       country  =>  Poland ,
       city  =>  Warsaw 
   ];
}

Step 8 − Now, type http://localhost:8080/index.php?r=site/test-response in the address bar, you can see the following JSON response.

JSON response

Yii implements a browser redirection by sending a Location HTTP header. You can call the yiiwebResponse::redirect() method to redirect the user browser to a URL.

Step 9 − Modify the actionTestResponse function this way.

pubpc function actionTestResponse() {
   return $this->redirect( http://www.tutorialspoint.com/ );
}

Now, if you go to http://localhost:8080/index.php?r=site/test-response, your browser will be redirected at the TutorialsPoint web site.

Sending Files

Yii provides the following methods to support file sending −

    yiiwebResponse::sendFile() − Sends an existing file.

    yiiwebResponse::sendStreamAsFile() − Sends an existing file stream as a file.

    yiiwebResponse::sendContentAsFile() − Sends a text string as a file.

Modify the actionTestResponse function this way −

pubpc function actionTestResponse() {
   return Yii::$app->response->sendFile( favicon.ico );
}

Type http://localhost:8080/index.php?r=site/test-response, you will see a download dialog window for the favicon.ico file −

favicon.ico File

The response is not sent until the yiiwebResponse::send() function is called. By default, this method is called at the end of the yiiaseApppcation::run() method. To send a response, the yiiwebResponse::send() method follows these steps −

    Triggers the yiiwebResponse::EVENT_BEFORE_SEND event.

    Calls the yiiwebResponse::prepare() method.

    Triggers the yiiwebResponse::EVENT_AFTER_PREPARE event.

    Calls the yiiwebResponse::sendHeaders() method.

    Calls the yiiwebResponse::sendContent() method.

    Triggers the yiiwebResponse::EVENT_AFTER_SEND event.

Advertisements