×ðÁú¿­Ê±¹ÙÍøµÇ¼

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°æ±¾¿ØÖÆ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ