Yii2 compared to YII1, a major improvement was built-in restful support with complete functionality.
Its built-in restful support provides the following features:
- Use ActiveRecord's common interface to quickly build prototypes;
- Reply Format negotiation (JSON and XML are supported by default);
- Customizable object serialization, which supports selecting which columns to output;
- The format of the request data and the validation error;
- Efficient routing via HTTP verb mapping;
- Support OPTIONS and HEAD verbs;
- Certification and authentication;
- Support for HATEOAS (RESTful schema constraints, hypermedia as application state);
- Result cache, can use Yii\filters\httpcache cache;
- Access rate limits to avoid malicious attacks and service abuse;
- Search and filter:
- Built-in data paging function;
- API version control.
These functions, if they are to be implemented by themselves, but also to have good extensibility, customization,
It still takes a lot of energy, but fortunately YII2 has been well implemented and is easy to expand.
Here is a brief introduction to some of the features:
The customizable output in Yii2 is simple, such as trying to output some columns or excluding certain columns, simply overwrite the fields method of the active record.
The support for API versioning in YII2 combines two popular approaches, one for embedding a version number in a URL (such as http://api.techbrood.com/v1/users), and one for specifying it in the HTTP accept header.
YII2 supports a large version, while the latter supports a minor version number. This provides a good balance between simplifying code, separating code, and reusing it.
The security mechanisms in YII2 support 3 ways, HTTP BASIC AUTH, query parameters (that is, include access tokens in URL parameters), and open source protocol OAUTH2.
The following is a complete Web service that uses a minimal number of statements to implement a user resource:
First, create a controller class app\controllers\UserController
:
Namespace App\controllers;use Yii\rest\activecontroller;class Usercontroller extends Activecontroller {public $ Modelclass = ' App\models\user ';}
Then, modify the Urlmanager configuration entry in config:
' Urlmanager ' + = [ ' enableprettyurl ' = ' = True, ' enablestrictparsing ' = ' = ', ' showscriptname ' = > False, ' rules ' = [ [' class ' = ' Yii\rest\urlrule ', ' controller ' = ' user '], ]
It's all right! You can try to access the following: Http://localhost/users in your browser.
This is just the user list interface, Yii2 actually automatically helps you create a complete set of interfaces:
Full features Please refer to the YII2 Chinese Guide RESTful section:Yii2 Chinese authoritative guide
by Iefreer