.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 刪除指定班