尊龙凯时人生就是博

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

13452372176

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ