ÔõÑùʹÓÃThinkPHP6¾ÙÐÐAPI½Ó¿ÚÎĵµÖÎÀí£¿
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬web api£¨Ó¦ÓóÌÐò½Ó¿Ú£©Ô½À´Ô½³£¼û£¬Ò²Ô½À´Ô½Ö÷Òª¡£¶ø¹ØÓÚÒ»¸öweb apiµÄÌṩÕ߶øÑÔ£¬±àдÍêÕûÇÒÒ×ÓÚÃ÷È·µÄapiÎĵµÊǺÜÊÇÓÐÐëÒªµÄ¡£¶øÏÖÔÚ£¬ÓÐÐí¶à¹¤¾ß¿ÉÒÔÇáËɵØÌìÉúapiÎĵµ£¬ÆäÖÐ×îÊ¢ÐеÄÊÇswagger¡£µ«ÔÚ±¾ÎÄÖУ¬ÎÒ½«ÖصãÏÈÈÝÔõÑùʹÓÃthinkphp6¿ò¼ÜÖÐÌṩµÄapi½Ó¿ÚÎĵµÖÎÀíÀ´ÖÎÀíapiÎĵµ¡£
×°ÖÃÎĵµÖÎÀíÀ©Õ¹
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚThinkPHP6µÄÏîÄ¿ÖÐ×°ÖÃAPIÎĵµÖÎÀíÀ©Õ¹£¬Ëü±»³ÆΪ”topthink/think-apidoc”¡£Äã¿ÉÒÔÔÚÏîÄ¿¸ùĿ¼ÏÂʹÓÃComposerÏÂÁîÐй¤¾ß¾ÙÐÐ×°Öãº
composer require topthink/think-apidoc
µÇ¼ºó¸´ÖÆ
±àдAPI½Ó¿ÚÎĵµ
×°ÖÃÍê³Éºó£¬ÎÒÃǾͿÉÒÔ×îÏȱàдAPI½Ó¿ÚÎĵµÁË¡£ÔÚThinkPHP6ÖУ¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷µÄÒªÁìÖÐʹÓÃ×¢Ê͵ķ½·¨À´±àдAPI½Ó¿ÚÎĵµ¡£ÀýÈ磺
/** * »ñÈ¡Óû§ÐÅÏ¢ * * @ApiTitle (»ñÈ¡Óû§ÐÅÏ¢) * @ApiSummary (ͨ¹ýÓû§ID»ñÈ¡Óû§ÐÅÏ¢) * @ApiMethod (GET) * @ApiRoute (/user/:id) * @ApiParams (name="id", type="integer", required=true, description="Óû§ID") * @ApiReturn ({"code": 200, "msg": "success", "data": {"id": 1, "name": "ÕÅÈý", "age": 18}}) * @ApiHeaders (name="Authorization", type="string", required=true, description="Óû§ÊÚȨToken") */ public function getUserInfo($id) { // TODO: »ñÈ¡Óû§ÐÅÏ¢µÄÂß¼ }
µÇ¼ºó¸´ÖÆ
ÉÏÊö×¢ÊÍÖУ¬ÎÒÃÇʹÓÃÁËһЩ²î±ðµÄ×¢½âÀ´ÐÎòAPI½Ó¿Ú£º
@ApiTitle£º½Ó¿ÚÃû³Æ
@ApiSummary£º½Ó¿Ú¼ò½é
@ApiMethod£ºÇëÇóÒªÁ죨GET¡¢POST¡¢PUTµÈ£©
@ApiRoute£º½Ó¿Ú·ÓÉ£¨ÀýÈç”/user/:id”£¬ÆäÖД:id”ÌåÏÖ¶¯Ì¬²ÎÊý£©
@ApiParams£º½Ó¿Ú²ÎÊý£¬ÆäÖаüÀ¨²ÎÊýÃû³Æ¡¢²ÎÊýÀàÐÍ¡¢ÊÇ·ñ±ØÌîÒÔ¼°²ÎÊý˵Ã÷µÈ
@ApiReturn£º½Ó¿Ú·µ»ØÖµ£¬°üÀ¨·µ»ØÖµµÄÃûÌÃÒÔ¼°·µ»ØÖµµÄ˵Ã÷
@ApiHeaders£º½Ó¿ÚÍ·²¿ÐÅÏ¢£¨ÀýÈçAuthorization£©
ÓÐÁËÉÏÊö×¢ÊÍ£¬ÎÒÃǾÍÄܹ»ÇåÎúµØÐÎòһ¸öAPI½Ó¿ÚµÄ»ù±¾ÐÅÏ¢ÁË¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÌìÉúAPIÎĵµ
±àдÍêAPI½Ó¿ÚÎĵµÖ®ºó£¬ÎÒÃǾͿÉÒÔʹÓÃThinkPHP6ÌṩµÄÏÂÁîÐй¤¾ßÌìÉúAPIÎĵµÁË¡£Ö»ÐèÒªÔÚÏîÄ¿¸ùĿ¼ÖУ¬ÔËÐÐÒÔÏÂÏÂÁî¼´¿É£º
php think apidoc --module api --path ./public/apidoc --type json
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁîÖУ¬ÎÒÃÇÖ¸¶¨ÁËapidoµÄ´æ´¢Â·¾¶ÒÔ¼°ÌìÉúµÄÎĵµÀàÐÍ£¨ÕâÀïÑ¡ÔñµÄÊÇjsonÃûÌã©¡£×¢ÖØ£¬ÎÒÃÇ»¹Ö¸¶¨ÁË–module²ÎÊýΪ”api”£¬ÕâÒâζ×ÅÎÒÃǽöÌìÉú”api”Ä£¿éµÄAPIÎĵµ¡£ÔÚÏÖʵӦÓÃÖУ¬¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐÑ¡Ôñ¡£
ÔËÐÐÉÏÊöÏÂÁîºó£¬ÎÒÃǾͿÉÒÔÔÚÖ¸¶¨µÄ´æ´¢Â·¾¶ÖÐÕÒµ½ÌìÉúµÄAPIÎĵµ¡£´Ëʱ£¬ÎÒÃÇ¿ÉÒÔ½«ËüÃÇת´ï¸ø½Ó¿ÚʹÓÃÕߣ¬Àû±ãËûÃÇÏàʶAPI½Ó¿ÚµÄ»ù±¾ÐÅÏ¢¡£
˼Ë÷Ì⣺
ÈôÊÇÄãÔÚÒ»¸öÒÑÓеÄÏîÄ¿ÖУ¬Ê¹ÓÃÎĵµÖÎÀíÀ©Õ¹£¬ÔÚ¶ÔÓ¦µÄ¿ØÖÆÆ÷ºÍÒªÁìÒªÁ춼¼ÓÉÏÁË×¢ÊÍ£¬´ËʱÄãÔÙÖ´ÐеÚÈý²½µÄ²Ù×÷£¬ÄãÆÚÍûAPI½Ó¿ÚÎĵµµÄÌìÉúЧ¹û³¤³ÉʲôÑù×Ó£¿
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP6¾ÙÐÐAPI½Ó¿ÚÎĵµÖÎÀí£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡