Yii2 high-speed building restful Web Services features simple introduction

Source: Internet
Author: User
Tags object serialization yii

Yii2 compared to YII1, a major improvement was built-in restful support with complete functionality.

Its built-in restful support provides for example the following features:

    • Use ActiveRecord's common interface to build prototypes at high speed;
    • 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);
    • The result cache. You can use the Yii\filters\httpcache cache.
    • Access rate limits to avoid malicious attacks and service abuse;
    • Search and filter:
    • Built-in data paging function;
    • API version number control.

These functional assumptions are self-fulfilling and have good extensibility, customization,

Still have to spend a lot of energy, just fortunately Yii2 has been very good to achieve and also very easy to expand.

Here is a brief introduction to some of the features:

The customizable output in Yii2 is very simple, for example, to output some columns or exclude certain columns. Simply the fields method of overwriting (override) the activity record is required.

Support for API version number control in YII2 combines two popular approaches, one for embedding a version number (such as http://api.techbrood.com/v1/users) in a URL, and one for specifying it in the HTTP accept header.

YII2 has supported the large version number, while the latter supports the minor version number. This provides a good balance between simplifying code, separating code, and reusing it.

The security mechanism in YII2 supports 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 the fewest 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, change the Urlmanager configuration item in config:

' Urlmanager ' + = [    ' enableprettyurl ' = ' = True,    ' enablestrictparsing '    = ' = ', ' showscriptname ' = > False,    ' rules ' = [        [' class ' = ' Yii\rest\urlrule ', ' controller ' = ' user '],    ]

That's it!

You can try to access the following in your browser: http://localhost/users.

This is just the user list interface, Yii2 actually volunteered to help you create a complete set of interfaces:


Full features Please refer to the YII2 Chinese Guide RESTful section:Yii2 Chinese authoritative guide


by Iefreer

Yii2 high-speed building restful Web Services features simple introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.