php - 关于restful的一点疑问
为情所困
为情所困 2017-05-16 13:03:34
0
4
413

关于restful,tp3.2手册里的描述感觉挺直白明了的,如下

作用                    传统模式                    REST模式 
列举出所有的用户         GET /users/list            GET /users 
列出ID为1的用户信息      GET /users/show/id/1       GET /users/1 
插入一个新的用户         POST /users/add            POST /users 
更新ID为1的用户信息      POST /users/mdy/id/1       PUT /users/1 
删除ID为1的用户         POST /users/delete/id/1     DELETE /users/1 

get方法好理解,就是判断参数进行不同的操作,比如上面的是要所有用户还是单个用户信息,通过是否有id来判断,这点没问题,但是我现在的疑问是,其他的比如更新操作的呢,如果我有两个接口,一个是修改用户个人信息,一个是仅修改用户手机,两个都是修改用户信息,修改个人信息可以用PUT /users/1 ,但是修改用户手机呢,我是要多传入一个参数比如PUT /users/1/2来根据参数来进行不同的修改操作?还是再另外新建一个控制器比如PUT /usersPhone/1这样?如果是后者的话也太麻烦了吧?

为情所困
为情所困

reply all(4)
我想大声告诉你

Tell the truth. This is not a restful design principle at all. There must be no verbs in the path first...
put can have a body, so it can be placed in the body.

巴扎黑

The body of POST can take parameters.

For example, if you bring

{
    phone:"123456789"
}

With this parameter, the background can know that the mobile phone number needs to be modified.

左手右手慢动作

You can modify personal information through PUT /users/1. 1 corresponds to the user's id, which corresponds to the modification of a record. The user's mobile phone is a field in the user information. PUT /users/1 The user information you need to modify You still need to pass it through json for modification

我想大声告诉你

RESTful mode:
http(s)://server.com/app-name/{version}/{domain}/{rest-convention}
Here, {version} represents the version information of the api. {domain} is an area that you can use to define any technical (for example: security - allow specified users to access this area.) or business reasons. (For example: the same functions are under the same prefix.)
{rest-convention} represents the agreed set of REST interfaces in this domain.

REST interface specification:
http://www.coderli.com/transl...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!