- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Responses
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.
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.
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.
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.
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 −
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.