ThinkPHP6ÖÐÔõÑùʵÏÖAPI°æ±¾¿ØÖÆ£¿
Ëæ×ÅwebÊÖÒÕµÄÉú³¤ºÍÓ¦Óó¡¾°µÄÒ»Ö±À©´ó£¬apiÒѾ³ÉΪÐí¶àÆóÒµÓ뿪·¢ÕßµÄÖ÷ÒªµÄÊÖÒÕ×é³É²¿·Ö£¬°æ±¾¿ØÖÆÒ²³ÉΪapiÉè¼ÆµÄ¹æ·¶Ö®Ò»¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6¿ò¼ÜÖÐʵÏÖapi°æ±¾¿ØÖÆ¡£
Ïàʶ°æ±¾¿ØÖÆ
°æ±¾¿ØÖÆÊÇÒ»ÖÖά»¤´úÂë»òÎĵµ±ä»»ÀúÊ·¼Í¼µÄ·½·¨¡£ÔÚAPIÉè¼ÆÖУ¬°æ±¾¿ØÖÆÊÇÈ·±£API²»»áÔÚÈκÎÇéÐÎÏÂÏòºó¼æÈݵÄÖ÷Òª·½·¨¡£
°æ±¾¿ØÖÆͨ³£ÓÐÒÔϼ¸ÖÖÀàÐÍ£º
URL°æ±¾¿ØÖÆ£ºÊ¹Óòî±ð°æ±¾µÄURLÀ´Çø·ÖAPI°æ±¾¡£
Header°æ±¾¿ØÖÆ£ºÊ¹ÓÃHTTP HeaderÀ´Çø·ÖAPI°æ±¾¡£
Query²ÎÊý°æ±¾¿ØÖÆ£ºÊ¹ÓÃÅÌÎʲÎÊýÀ´Ö¸¶¨API°æ±¾¡£
ÔÚThinkPHP6ÖУ¬ÎÒÃǽ«Ê¹ÓÃURL°æ±¾¿ØÖÆ·½·¨À´ÊµÏÖAPI°æ±¾¿ØÖÆ¡£
½¨Éè°æ±¾¿ØÖÆÖÐÐļþ
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»ÆäÖÐÐļþ£¬ÓÃÓÚ´¦ÀíºÍÑéÖ¤API°æ±¾¡£ÔÚThinkPHP6ÖУ¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´½¨ÉèÖÐÐļþ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
php think make:middleware VersionControl
µÇ¼ºó¸´ÖÆ
È»ºó£¬Æ¾Ö¤API°æ±¾ÔÚhandleÒªÁìÖÐʵÏÖ°æ±¾¿ØÖÆÂß¼¡£ÒÔURL°æ±¾¿ØÖÆΪÀý£¬ÎÒÃÇ¿ÉÒÔÔÚURLÖÐʹÓÃÒ»¸öǰ׺À´Ö¸¶¨API°æ±¾¡£ÀýÈ磬ʹÓÃ/v1/userÀ´»á¼û°æ±¾1µÄuserAPI¡£
ÖÐÐļþ´úÂëÈçÏÂËùʾ£º
<?php declare (strict_types = 1); namespace appmiddleware; use thinkRequest; use thinkResponse; class VersionControl { public function handle(Request $request, Closure $next) { $version = $request->param('version'); // »ñÈ¡°æ±¾ºÅ if (!$this->isValidVersion($version)) { $response = new Response(); $response->code(400); $response->data('Invalid Version'); // ·µ»Ø¹ýʧÏìÓ¦ return $response; } else { // Õý³£ÇëÇó return $next($request); } } private function isValidVersion($version) { // ÑéÖ¤°æ±¾ºÅÊÇ·ñÓÐÓã¬ÕâÀï¿ÉÒÔƾ֤×Ô¼ºµÄÓªÒµ¹æÔò¾ÙÐÐÑéÖ¤ switch ($version) { case 'v1': case 'v2': return true; default: return false; } } }
µÇ¼ºó¸´ÖÆ
×¢²á°æ±¾¿ØÖÆÖÐÐļþ
ÖÐÐļþ½¨ÉèÍê³Éºó£¬ÎÒÃÇÐèÒª½«Æä×¢²áµ½Ó¦ÓÃÖУ¬ÒÔ±ã¶ÔÇëÇó¾ÙÐÐ×èµ²ºÍ´¦Àí¡£ÔÚÓ¦ÓõÄmiddleware.phpÎļþÖÐÌí¼ÓÖÐÐļþÀàµÄÈ«Ãû£¬²¢Ö¸¶¨ÐèÒªÖÐÐļþ´¦ÀíµÄ·ÓÉǰ׺¡£
return [ 'VersionControl' => ppmiddlewareVersionControl::class, ]; // ·ÓÉÖÐÐļþ return [ 'api/:version/user' => 'api/:version.User/index', 'api/:version/order' => 'api/:version.Order/index', 'api/:version/product' => 'api/:version.Product/index', ]->middleware(['VersionControl']);
µÇ¼ºó¸´ÖÆ
ʵÏÖ°æ±¾»¯µÄAPI
ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔʵÏÖ°üÀ¨°æ±¾ºÅµÄAPI¡£½¨Éè°æ±¾ºÅ¿ØÖÆÆ÷ºÍ¶ÔÓ¦µÄÒªÁ죬ÔÚÒªÁìÖÐʵÏÖÓªÒµÂß¼¡£
ÀýÈ磬ÔÚ¿ØÖÆÆ÷apppicontroller1User.phpÖУ¬ÎÒÃÇ¿ÉÒÔʵÏÖAPIµÄ°æ±¾¿ØÖÆÒªÁ죺
<?php declare (strict_types = 1); namespace apppicontroller1; use thinkesponseJson; class User { public function index(): Json { return json(['message' => 'This is user API V1']); } }
µÇ¼ºó¸´ÖÆ
ÔÚapppicontroller2User.phpÖУ¬ÊµÏÖAPI°æ±¾2µÄ¿ØÖÆÆ÷ÒªÁ죺
<?php declare (strict_types = 1); namespace apppicontroller2; use thinkesponseJson; class User { public function index(): Json { return json(['message' => 'This is user API V2']); } }
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃǾͿÉÒÔÔÚä¯ÀÀÆ÷Öлá¼û²î±ð°æ±¾µÄÓû§API£º
http://localhost/api/v1/user£º·µ»Ø{“message”:”This is user API V1″}
http://localhost/api/v2/user£º·µ»Ø{“message”:”This is user API V2″}
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËThinkPHP6ÖÐÔõÑùʵÏÖAPI°æ±¾¿ØÖƵÄÒªÁ죬Ö÷ҪʹÓÃURL°æ±¾¿ØÖƵķ½·¨£¬²¢Í¨¹ý½¨ÉèÖÐÐļþ¡¢×¢²áÖÐÐļþºÍ°æ±¾»¯µÄAPIʵÏÖ°æ±¾¿ØÖÆ¡£
°æȨÉùÃ÷£º±¾ÎÄΪ×÷ÕßÔ´´£¬Î´¾×÷ÕßÊÚȨ£¬²»µÃתÔØ¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑùʵÏÖAPI°æ±¾¿ØÖÆ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡