ÔõÑùʹÓÃThinkPHP6¾ÙÐÐRESTful API²âÊÔ£¿
Ëæ×ÅÒƶ¯»¥ÁªÍøµÄ¿ìËÙÉú³¤ºÍÔÆÅÌËãµÄÆÕ¼°£¬webЧÀÍ£¨ÌØÊâÊÇrestful api£©ÒѳÉΪÏÖÔÚ¿ª·¢ÁìÓòÖÐ×îÖ÷ÒªµÄÒ»²¿·Ö¡£ÄÇôÔõÑùʹÓÃthinkphp6¾ÙÐÐrestful api²âÊÔÄØ£¿±¾ÎĽ«ÏêϸÏÈÈÝthinkphp6ÖеÄrestful api²âÊÔÒªÁìÒÔ¼°½¨ÒéµÄ¹¤¾ßºÍʵ¼ù¡£
ÇéÐδ
Ê×ÏÈ£¬ÐèҪװÖÃThinkPHP6ÇéÐΣ¬¿ÉÒÔʹÓùÙÍøÌṩµÄcomposer¾ÙÐÐ×°Öá£ÔÚÏÂÁîÐд°¿ÚÖÐÊäÈëÒÔÏÂÏÂÁî¼´¿É£º
composer create-project topthink/think tp6
µÇ¼ºó¸´ÖÆ
½Ó×Å£¬ÔÚÏîÄ¿¸ùĿ¼Ï½¨Éè.envÎļþ£¬ÄÚÀïÐèÒª¼ÓÈëÊý¾Ý¿âÉèÖãº
DB_HOST = localhost DB_NAME = test DB_USER = root DB_PASSWORD =
µÇ¼ºó¸´ÖÆ
·Óɽç˵
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRoute::ruleÒªÁìÀ´½ç˵·ÓÉ£¬ÀýÈ磺
Route::rule('users', 'apppicontrollerUser');
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬usersÊÇÎÒÃÇ×Ô½ç˵µÄURI·¾¶£¬apppicontrollerUserÔòÊǶÔÓ¦µÄ¿ØÖÆÆ÷¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
±àд¿ØÖÆÆ÷
ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ý¿ØÖÆÆ÷£¨Controller£©À´´¦ÀíRESTful APIÇëÇó¡£ÒÔÏÂÊÇÒ»¸ö¼òÆӵĿØÖÆÆ÷´úÂ룺
<?php namespace apppicontroller; use thinkacadeDb; class User { public function index() { return json(Db::table('users')->select()); } public function read($id) { return json(Db::table('users')->where('id', $id)->find()); } public function save() { $data = input(); Db::table('users')->insert($data); return json(['msg' => 'created']); } public function update($id) { $data = input(); Db::table('users')->where('id', $id)->update($data); return json(['msg' => 'updated']); } public function delete($id) { Db::table('users')->where('id', $id)->delete(); return json(['msg' => 'deleted']); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸ö¿ØÖÆÆ÷ÖУ¬ÎÒÃǽç˵ÁËindex¡¢read¡¢save¡¢updateºÍdeleteÎå¸öÒªÁ죬»®·Ö¶ÔÓ¦RESTful APIÖеÄÎå¸öÒªÁ죺GET¡¢GET¡¢POST¡¢PUTºÍDELETE¡£
²âÊÔ¹¤¾ß
ÔÚ¾ÙÐÐRESTful API²âÊÔʱ£¬ÎÒÃÇÐèҪʹÓÃһЩ¹¤¾ßÀ´Ä£Äâ¶ÔAPIµÄÇëÇóºÍÏìÓ¦£¬ÒÔÏÂÊÇÆäÖм¸¿î³£¼ûµÄ²âÊÔ¹¤¾ß¡£
4.1 Postman
PostmanÊÇÒ»¿î¹¦Ð§Ç¿Ê¢µÄAPI¿ª·¢²âÊÔ¹¤¾ß£¬Ö§³Ö¶àÖÖHTTPÇëÇóÀàÐÍ£¬Àû±ãÒ×Ó㬿ÉÇáËɵØÄ£Äâ·¢ËÍHTTPÇëÇó²¢Éó²éÏìÓ¦¡£
4.2 cURL
cURL ÊÇÒ»¸ö³£ÓõÄÏÂÁîÐй¤¾ß£¬ÓÃÓÚ´«ÊäÊý¾Ý£¬Ö§³Ö¶àÖÖÐÒ飬°üÀ¨HTTP¡¢FTP¡¢SMTPµÈ¡£Ê¹ÓÃcURL¿ÉÒÔ¾ÙÐÐÖÖÖÖHTTPÇëÇó²Ù×÷¡£
4.3 Advanced REST Client
Advanced REST ClientÊÇÒ»¿îÀ©Õ¹ÐÔÇ¿µÄä¯ÀÀÆ÷²å¼þ£¬Í¨¹ý¾«Á·µÄUIºÍÐí¶à±ãµ±µÄ¹¦Ð§£¬ÊµÏÖÁËÇáËɵÄRESTful API²âÊÔ£¬Î´À´Éú³¤Ç±Á¦ºÜ´ó¡£
ʵ¼ù°¸Àý
ÔÚÏÖʵÏîÄ¿ÖУ¬RESTful API²âÊÔÐèÒªÑÏ¿á×ñÊؽӿÚÎĵµ£¬ºÏÀíʹÓÃÖÖÖÖHTTPÇëÇóÀàÐͺÍÏìÓ¦Â룬ÒÔÏÂÊÇÒ»¸ö¼òÆӵݸÀý£º
5.1 HTTP GETÇëÇó
URI£ºhttp://localhost/api/users
Method£ºGET
ÏìӦ״̬Â룺200
ÏìÓ¦Êý¾ÝÃûÌãºjson
[ { "id": 1, "name": "Tom", "email": "tom@example.com" }, { "id": 2, "name": "Jerry", "email": "jerry@example.com" } ]
µÇ¼ºó¸´ÖÆ
5.2 HTTP POSTÇëÇó
URI£ºhttp://localhost/api/users
Method£ºPOST
ÇëÇóÊý¾ÝÃûÌãºform-data
ÇëÇóÊý¾Ý²ÎÊý£º
²ÎÊýÃû | ²ÎÊýÖµ |
---|---|
name | Mary |
mary@example.com |
ÏìӦ״̬Â룺201
ÏìÓ¦Êý¾ÝÃûÌãºjson
{ "msg": "created" }
µÇ¼ºó¸´ÖÆ
5.3 HTTP PUTÇëÇó
URI£ºhttp://localhost/api/users/3
Method£ºPUT
ÇëÇóÊý¾ÝÃûÌãºx-www-form-urlencoded
ÇëÇóÊý¾Ý²ÎÊý£º
²ÎÊýÃû | ²ÎÊýÖµ |
---|---|
name | John |
john@example.com |
ÏìӦ״̬Â룺200
ÏìÓ¦Êý¾ÝÃûÌãºjson
{ "msg": "updated" }
µÇ¼ºó¸´ÖÆ
5.4 HTTP DELETEÇëÇó
URI£ºhttp://localhost/api/users/3
Method£ºDELETE
ÏìӦ״̬Â룺200
ÏìÓ¦Êý¾ÝÃûÌãºjson
{ "msg": "deleted" }
µÇ¼ºó¸´ÖÆ
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃThinkPHP6¾ÙÐÐRESTful API²âÊÔ¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒª´î½¨ÇéÐΡ¢½ç˵·Óɺͱàд¿ØÖÆÆ÷¡£È»ºó£¬ÎÒÃÇÍƼöÁ˼¸¿î³£¼ûµÄ²âÊÔ¹¤¾ß£¬²¢Í¨¹ýÒ»¸öʵ¼ù°¸Àý£¬Õ¹Ê¾ÁËÔõÑù¾ÙÐÐHTTP GET¡¢POST¡¢PUTºÍDELETEÇëÇó£¬ÒÔ¼°ÏìÓ¦µÄÏìӦ״̬ÂëºÍÊý¾ÝÃûÌá£Ï£Íû¶ÁÕß¿ÉÒÔÉîÈëѧϰThinkPHP6£¬ÔËÓúÃRESTful API²âÊÔ£¬Ìá¸ß¿ª·¢Ð§ÂÊÒÔ¼°ÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐRESTful API²âÊÔ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡