Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel SwaggerÌìÉúAPIÎĵµ£¿
laravel¿ª·¢£ºÔõÑùʹÓÃlaravel swaggerÌìÉúapiÎĵµ£¿
ÔÚ¿ª·¢ Web Ó¦ÓóÌÐòʱ£¬´¦Àí API ÎĵµÍùÍùÊÇÒ»Ïî·±Ëöµ«±Ø²»¿ÉÉÙµÄʹÃü¡£Ê¹Óà Swagger ¿ÉÒÔ×Ô¶¯ÌìÉú API Îĵµ²¢Ê¹Æä¿ÉÊÓ»¯¡£ÔÚ Laravel ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà Laravel Swagger À©Õ¹°üÀ´ÇáËɵØÌìÉú Swagger API Îĵµ¡£±¾ÎĽ«Ö¸ÒýÄúÔõÑùÔÚ Laravel ÖÐʹÓà Laravel Swagger¡£
×°ÖÃ Laravel Swagger
ʹÓà Composer ×°Öà Laravel Swagger À©Õ¹°ü£º
composer require darkaonline/l5-swagger
µÇ¼ºó¸´ÖÆ
ÉèÖÃ Laravel Swagger
Laravel Swagger ÒÀÀµÓÚ Swagger UI£¬Òò´ËÎÒÃÇÐèÒª½« Swagger UI µÄ×ÊÔ´Ðû²¼µ½ Laravel µÄ¹«¹²Ä¿Â¼ÖУ¬Ê¹ÓÃÒÔÏÂÏÂÁîÍê³ÉÐû²¼£º
php artisan vendor:publish --provider "L5SwaggerL5SwaggerServiceProvider"
µÇ¼ºó¸´ÖÆ
Ö´ÐÐÐû²¼ÏÂÁîºó£¬½«»áÔÚ public/vendor Ŀ¼Ï¿´µ½ swagger-ui Ŀ¼£¬Õâ¸öĿ¼ÖаüÀ¨ÁË Swagger UI µÄËùÓÐ×ÊÔ´¡£
½ÓÏÂÀ´£¬ÔÚ Laravel µÄÉèÖÃÎļþ config/app.php ÖÐÌí¼ÓÒÔÏÂÐУº
'providers' => [ ... L5SwaggerL5SwaggerServiceProvider::class, ], 'aliases' => [ ... 'Swagger' => L5SwaggerFacadesL5Swagger::class, ],
µÇ¼ºó¸´ÖÆ
Ìí¼Ó Swagger ×¢ÊÍ
ΪÁ˸æËß Laravel Swagger ûÓÐÍÆ¶ÏµÄ API ÃûÌã¬ÎÒÃÇÐèÒªÔÚ´úÂëÖÐÌí¼Ó Swagger ×¢ÊÍ¡£ÕâЩעÊÍ¿ÉÒÔÈà Laravel Swagger ×Ô¶¯ÆÊÎöÄúµÄ API£¬²¢ÌìÉú¶ÔÓ¦µÄÎĵµ¡£
/** * @OAGet( * path="/users", * operationId="getUsersList", * tags={"Users"}, * summary="Get list of registered users", * description="Returns list of users", * @OAResponse(response="200", description="successful operation"), * @OAResponse(response=401, description="Unauthorized"), * @OAResponse(response=403, description="Forbidden"), * @OAResponse(response=404, description="Not Found"), * @OAResponse(response=500, description="Internal Server Error") * ) */
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓà @OAGet ×¢ÊÍÌåÏÖÕâÊÇÒ»¸ö GET ÇëÇó¡£path ×¢Êͽç˵ API µÄ·¾¶¡£tags ºÍ summary ×¢ÊÍÓÃÓÚÔÚ Swagger ÎĵµÖÐÏÔʾժҪºÍ±êÇ©¡£×îºó£¬@OAResponse ×¢ÊÍʾÀýÁË¿ÉÄܵÄÏìӦ״̬¡£
ÔÚ Laravel ÖÐÉó²é Swagger Îĵµ
ÔÚÍê³ÉËùÓÐÏÈÇ°µÄ°ì·¨Ö®ºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ URL À´Éó²é Laravel Swagger Îĵµ£º
http://your-app.dev/api/documentation
µÇ¼ºó¸´ÖÆ
£¨Çë×¢ÖØ£¬ÈôÊÇÄúʹÓõÄÊÇ Laravel 5.4 »òÒÔÉÏ°æ±¾£¬ÔòÎÞÐè½ç˵ .dev£¬Çë¸ÄΪʹÓà .test »òÆäËûÍâµØÇøÃû£©
Æô¶¯ Laravel µÄ¿ª·¢Ð§ÀÍÆ÷£¬²¢»á¼ûÉÏÃæµÄ URL£¬ÄúÓ¦¸ÃÄܹ»ÔÚä¯ÀÀÆ÷Öп´µ½×Ô¶¯ÌìÉúµÄ Swagger Îĵµ¡£
ÔÚ Swagger ÎĵµÖУ¬Äú¿ÉÒÔÉó²é½ç˵µÄ API£¬Æ¾Ö¤ API ÖÐÌí¼ÓµÄ Swagger ×¢ÊÍÀ´²âÊÔ API£¬²¢Éó²é¿ÉÄܵÄÏìӦ״̬¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏàʶÁËÔõÑùʹÓà Laravel Swagger À©Õ¹°üÇáËÉÌìÉú Swagger API Îĵµ¡£Ê×ÏÈ£¬ÎÒÃÇ×°ÖÃÁË Laravel Swagger£¬È»ºóÆô¶¯ Swagger£¬²¢Îª API Ìí¼ÓÁË Swagger ×¢ÊÍ¡£×îºó£¬ÎÒÃÇÉó²éÁË Laravel Swagger ÌìÉúµÄÎĵµ¡£
ʹÓà Laravel Swagger ¿ÉÒÔ´ó´ó¼õÇáÊÖ¶¯±àд API ÎĵµµÄ¼ç¸º£¬×èÖ¹ÁË¿ÉÄܵĹýʧºÍ·×ÆçÖÂÐÔ¡£Í¨¹ýʹÓà Swagger UI£¬ÎÒÃÇ¿ÉÒÔ¸üÀû±ãµØÉó²éºÍ²âÊÔ API£¬Í¬Ê±ÌṩÁ˶Կª·¢Ö°Ô±ÓѺõĽӿڡ£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel SwaggerÌìÉúAPIÎĵµ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡