পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, March 22, 2017

Yii - Responses

When a web application 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 application component. By default, it is an instance of yii\web\Response.

To manage response HTTP status codes, use the yii\web\Response::$statusCode property. The default value of yii\web\Response::$statusCode is 200.
Step 1 − Add a function named actionTestResponse to the SiteController.
public function actionTestResponse() {
   Yii::$appresponse->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 −
  • yii\web\BadRequestHttpException − status code 400.
  • yii\web\UnauthorizedHttpException − status code 401.
  • yii\web\ForbiddenHttpException − status code 403.
  • yii\web\NotFoundHttpException − status code 404.
  • yii\web\MethodNotAllowedHttpException − status code 405.
  • yii\web\NotAcceptableHttpException − status code 406.
  • yii\web\ConflictHttpException − status code 409.
  • yii\web\GoneHttpException − status code 410.
  • yii\web\UnsupportedMediaTypeHttpException − status code 415.
  • yii\web\TooManyRequestsHttpException − status code 429.
  • yii\web\ServerErrorHttpException − status code 500.
Step 3 − Modify the actionTestResponse function as shown in the following code.
public function actionTestResponse() {
   throw new \yii\web\GoneHttpException;
}
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.
public 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 yii\web\HtmlResponseFormatter.
  • XML − implemented by yii\web\XmlResponseFormatter.
  • JSON − implemented by yii\web\JsonResponseFormatter.
  • JSONP − implemented by yii\web\JsonResponseFormatter.
  • RAW − the response without any formatting.
Step 7 − To respond in the JSON format, modify the actionTestResponse function.
public function actionTestResponse() {
   \Yii::$app->response->format = \yii\web\Response::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 yii\web\Response::redirect() method to redirect the user browser to a URL.
Step 9 − Modify the actionTestResponse function this way.
public 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 −
  • yii\web\Response::sendFile() − Sends an existing file.
  • yii\web\Response::sendStreamAsFile() − Sends an existing file stream as a file.
  • yii\web\Response::sendContentAsFile() − Sends a text string as a file.
Modify the actionTestResponse function this way −
public 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 yii\web\Response::send() function is called. By default, this method is called at the end of the yii\base\Application::run() method. To send a response, the yii\web\Response::send() method follows these steps −
  • Triggers the yii\web\Response::EVENT_BEFORE_SEND event.
  • Calls the yii\web\Response::prepare() method.
  • Triggers the yii\web\Response::EVENT_AFTER_PREPARE event.
  • Calls the yii\web\Response::sendHeaders() method.
  • Calls the yii\web\Response::sendContent() method.
  • Triggers the yii\web\Response::EVENT_AFTER_SEND event.

No comments:

Post a Comment