Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: the landlord should have a basic understanding of REST, so I will not repeat the basic concepts. I just want to talk about the concept of REST.
Resources are not directly mapped to underlying storage objects or program models. It doesn't mean that you have a User table and a Role table, you must design the corresponding resources.
In fact, the relationship between RESTful resources and underlying storage services is similar to the relationship between tables and views in relational databases. a view is a set of relationships formed by combining multiple tables according to the actual query needs.
Whether your storage service is a relational database, NoSQL database, or even text file, it is the same for the client that accesses resources.
Therefore, to create a user and set its role, you can directly use POST/user.
// Create a new user with the foo and bar roles
POST/user
{Name: (string), passwd: (string), roles: ['foo', 'bar']}
// It is best if the response header can contain the following two
// Respond in the 201 status and use Location to notify the new resource url
HTTP/1.1 201 Created
Location:/user/1
---------------------------------------------------------
// Modify the role of a user to foobar.
PUT/user/1
{Roles: ['foobar']}
---------------------------------------------------------
// Modify the user's password
PUT/user/1
{Passwd: (string )}
For resources with a small granularity such as/UserRoleRelation, I suggest you do not first. the granularity of resources should be rough and fine, based on the subsequent business evolution and actual needs, we need to consider whether to abstract more fine-grained resources. if we get too detailed at the beginning, any operation will be broken down into multiple network I/O operations, and the system complexity is relatively high.
Map a specific database table to a resource, and then map the CRUD action to GET/POST/PUT/DELETE, which is silly and insecure. these items are originally used for business services, because of business requirements, the results are abstracted but not designed around the business.
The correct idea should be: what databases and program models should be forgotten, and what resources (URLs) should be designed based on the service from the HTTP perspective ), what parameters are accepted and responded to in GET/POST? after finalizing these parameters, we will consider how to cooperate with the database and program Model. There are video tutorials on Lynda to teach you how to design restful APIs.
Proactive Design of RESTful APIs
From the existing framework, operations on resources are generally mapped to object methods. Resource design is coarse-grained, rather than designing resources from the underlying database. A mature framework is used for RESTful design. generally, it is a resource-object-relational database, which contains two-layer ING. As mentioned above, the RESTFUL system design should start from the business rather than the underlying database.
First, consider the business scope and functions of your system, then classify database CRUD into business operations, and consider how the database works with the business process.
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.