Let's tell you what rest is.
Restful
Rest full name is representational state Transfer, the Chinese meaning is the expression (Editor Note: usually translated as a representation) of sexual status transfer. It first appeared in the 2000 Roy Fielding's doctoral thesis, and Roy Fielding was one of the main writers of the HTTP specification. "The purpose of my article is to understand and evaluate the architecture design of web-based applications based on the principles of architecture, and to get a powerful, good performance and suitable communication architecture," he said in his paper. Rest refers to a set of architectural constraints and principles. "If a schema conforms to rest constraints and principles, we call it the restful schema."
Rest itself does not create new technologies, components, or services, and the idea behind restful is to use the existing features and capabilities of the web to better use some of the guidelines and constraints in existing Web standards. While rest itself is deeply influenced by web technology, the rest architecture style is theoretically not bound to HTTP, except that HTTP is currently the only rest-related instance. So the rest that we're describing here is also a rest that is implemented via HTTP.
Not long ago to do a project, is to use the YII2 framework to write a set of RESTful style API, went to check the "YII 2.0 authoritative Guide", found that the above written more briefly. So just write a tutorial post here that will help you get started with the small partner who just contacted the YII2 framework restful.
First, directory structure
It takes only three files to implement a simple restful API. The directory is as follows:
Frontend
├─config
│└main.php
├─controllers
│└bookcontroller.php
└─models
Ii. Configuring URL Rules
1. Modify the server's rewrite rule to point all URLs to the index.php to support/BOOKS/1 format.
If it is an Apache server, create a new. htaccess file in the frontend/web/directory. The contents of the document are as follows:
Rewriteengine
on # If a directory or a file exists, use the request directly
Rewritecond%{request_filename}!-f
rewritecond%{request_filename}!-d
# Otherwise forward the REQUEST to index.php rewriterule
. index.php
If this is a nginx server, modify the nginx/conf/nginx.conf and add the following red markup in location/{} of the current "server{}":
Location/{
try_files $uri $uri//index.php$is_args$args;
}
2. Modify the frontend/config/main.php file to add a URL rule for the book controller. This allows access to and manipulation of data through landscaped URLs and meaningful HTTP verbs. The configuration is as follows:
' Components ' => ['
urlmanager ' => [
' Enableprettyurl ' => true,
' enablestrictparsing ' => true,< c4/> ' Showscriptname ' => false,
' rules ' => [
[' Class ' => ' Yii\rest\urlrule ', ' Controller ' => ' book '] ,
],
],
],
Third, create a model
1. Create a book table in the database. The Book table reads as follows:
------------------------------
--table structure for book
------------------------------
DROP table IF EXISTS ' book ';
CREATE TABLE ' book ' (
' id ' int (a) unsigned NOT null auto_increment,
' name ' char ' (+) NOT null DEFAULT ',
' num ' tinyint (3) unsigned not NULL default ' 0 ',
PRIMARY KEY (' id ')
) engine=innodb auto_increment=6 DEFAULT Charset=ut F8;
------------------------------
-Records of book
------------------------------
INSERT in ' book ' VALUES (' 1 ', ' toefl ', ' ten ');
INSERT into ' book ' VALUES (' 2 ', ' IELTS ', ') ';
INSERT into ' book ' VALUES (' 3 ', ' sat ', ') ';
INSERT into ' book ' VALUES (' 4 ', ' GRE ', ');
INSERT into ' book ' VALUES (' 5 ', ' GMAT ', ' 50 ');
2. Create a new book.php in the frontend/models/directory. The contents of the document are as follows:
namespace Frontend\models;
Use Yii\db\activerecord;
Class Book extends ActiveRecord
{public
static function tablename ()
{return
' book ';
}
}
Four, create a controller
Create a new bookcontroller.php in the frontend/controllers/directory. The controller class extends from Yii\rest\activecontroller. By specifying Yii\rest\activecontroller::modelclass as a Frontend\models\book, the controller can know which model to use to fetch and process the data. The contents of the document are as follows:
namespace Frontend\controllers;
Use Yii\rest\activecontroller;
Class Bookcontroller extends Activecontroller
{public
$modelClass = ' Frontend\models\book ';
}
Five, test
Here, we have finished creating the RESTful-style API for accessing user data. The APIs created include:
Get/books: List all the books
Head/books: Overview of displaying a list of books
Post/books: Add 1 books
GET/BOOKS/1: Return to the book id=1 details
HEAD/BOOKS/1: Overview of Display book id=1
PATCH/BOOKS/1 and PUT/BOOKS/1: More information about the new book Id=1
DELETE/BOOKS/1: Delete book id=1 information
Options/books: Displays verbs about end/books support
OPTIONS/BOOKS/1: Displays verbs about end/BOOKS/1 support
You can access the API by entering the URL http://{frontend domain}/books in a Web browser, or by using some browser plug-ins to send specific headers requests, such as Firefox restclient, Chrome Advanced Rest Client, Postman, and so on.
Vi. description
1.Yii automatically converts the name of the controller used at the end into a complex number. This is because Yii\rest\urlrule can be used for their terminal fully automatic complex controller. You can disable this behavior by setting the Yii\rest\urlrule::p luralize to False:
' Rules ' => [
[' Class ' => ' Yii\rest\urlrule ', ' Controller ' => ' book ', ' Pluralize ' => false],
]
2. You can use the fields and expand parameters to specify which fields should be included in the result. For example: The URL http://{frontend domain name}/books?fields=name,num will only return the name and Num fields.
The above is a small set to introduce the YII2 framework for the production of restful-style API Quick Start tutorials, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!