- 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 - Fields
By overriding fields() and extraFields() methods, you can define what data can be put into a response. The difference between these two methods is that the former defines the default set of fields, which should be included in the response while the latter defines additional fields, which may be included in the response if an end user requests for them via the expand query parameter.
Step 1 − Modify the MyUser model this way.
<?php namespace appmodels; use appcomponentsUppercaseBehavior; use Yii; /** * This is the model class for table "user". *@property integer $id * @property string $name * @property string $email */ class MyUser extends yiidbActiveRecord { pubpc function fields() { return [ id , name , //PHP callback datetime => function($model) { return date("d:m:Y H:i:s"); } ]; } /** * @inheritdoc */ pubpc static function tableName() { return user ; } /** * @inheritdoc */ pubpc function rules() { return [ [[ name , email ], string , max => 255] ]; } /** * @inheritdoc */ pubpc function attributeLabels() { return [ id => ID , name => Name , email => Email , ]; } } ?>
Besides default fields: id and name, we have added a custom field – datetime.
Step 2 − In Postman, run the URL http://localhost:8080/users.
![Run URL](/yii/images/run_url.jpg)
Step 3 − Now, modify the MyUser model this way.
<?php namespace appmodels; use appcomponentsUppercaseBehavior; use Yii; /** * This is the model class for table "user". * * @property integer $id * @property string $name * @property string $email */ class MyUser extends yiidbActiveRecord { pubpc function fields() { return [ id , name , ]; } pubpc function extraFields() { return [ email ]; } /** * @inheritdoc */ pubpc static function tableName() { return user ; } /** * @inheritdoc */ pubpc function rules() { return [ [[ name , email ], string , max => 255] ]; } /** * @inheritdoc */ pubpc function attributeLabels() { return [ id => ID , name => Name , email => Email , ]; } } ?>
Notice, that the email field is returned by the extraFields() method.
Step 4 − To get data with this field, run http://localhost:8080/users?expand=email.
![Get Data](/yii/images/get_data.jpg)
Customizing Actions
The yii estActiveController class provides the following actions −
Index − Lists resources page by page
View − Returns the details of a specified resource
Create − Creates a new resource
Update − Updates an existing resource
Delete − Deletes the specified resource
Options − Returns the supported HTTP methods
All above actions are declared in the actions method().
To disable the “delete” and “create” actions, modify the UserController this way −
<?php namespace appcontrollers; use yii estActiveController; class UserController extends ActiveController { pubpc $modelClass = appmodelsMyUser ; pubpc function actions() { $actions = parent::actions(); // disable the "delete" and "create" actions unset($actions[ delete ], $actions[ create ]); return $actions; } } ?>
Handpng Errors
When obtaining a RESTful API request, if there is an error in the request or something unexpected happens on the server, you may simply throw an exception. If you can identify the cause of the error, you should throw an exception along with a proper HTTP status code. Yii REST uses the following statuses −
200 − OK.
201 − A resource was successfully created in response to a POST request. The Location header contains the URL pointing to the newly created resource.
204 − The request was handled successfully and the response contains no content.
304 − The resource was not modified.
400 − Bad request.
401 − Authentication failed.
403 − The authenticated user is not allowed to access the specified API endpoint.
404 − The resource does not exist.
405 − Method not allowed.
415 − Unsupported media type.
422 − Data vapdation failed.
429 − Too many requests.
500 − Internal server error.