.htaccess :重写URL,使URL以 /restful/class/1 形式访问文件
- Options +FollowSymlinks
- RewriteEngine on
-
- # 重写规则
- RewriteRule ^class$ index.php?class=all [nc,qsa]
- RewriteRule ^class/(\d+)$ index.php?class=$1 [nc,qsa]
在Apache的web目录下新建一个restful目录,把这四个文件放入restful目录下就可以访问了。
可以通过接口测试工具来测试这些接口,比如Firefox的RestClient、Chrome的Advanced Rest Client、postman等。
测试方法:
- GET http://localhost/restful/class 列出所有班级
- GET http://localhost/restful/class/1 获取指定班的信息
- POST http://localhost/restful/class?name=SAT班&count=23 新建一个班
- PUT http://localhost/restful/class/1?name=SAT班&count=23 更新指定班的信息(全部信息)
- PATCH http://localhost/restful/class/1?name=SAT班 更新指定班的信息(部分信息)
- DELETE http://localhost/restful/class/1 删除指定班