YII2 Quick Build RESTful Web Services features 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 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

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.