我在 Lumen 微框架中构建了一些 REST API,它运行良好。现在我想将 Swagger 集成到其中,这样 API 将在 future 使用时得到很好的记录。有人做过吗?
最佳答案
我真的很难学会如何使用它。在这里,我将分享我为使其正常工作所做的一些事情
Use this wrapper
在终端中运行此命令:
composer require "darkaonline/swagger-lume:5.5.*"
或者如果这对您不起作用,则来自链接的 repo 中的旧版本
然后从 repo 执行以下步骤:
Open your bootstrap/app.php file and: uncomment this line (around line 26) in Create The Application section:
$app->withFacades(); add this line before Register Container Bindings section: $app->configure('swagger-lume'); add this line in Register Service Providers section: $app->register(\SwaggerLume\ServiceProvider::class);
然后你需要为你的项目使用注释而不是 YAML 或 JSON
For more
创建
Annotation.php
应用程序文件夹中的文件添加以下示例/**
* @SWG\Swagger(
* basePath="/",
* schemes={"http"},
* @SWG\Info(
* version="1.0.0",
* title="HAVE MY BOOKS",
* @SWG\Contact(
* email="example@test.com"
* ),
* )
* )
*/
/**
* @SWG\Get(
* path="/",
* summary="Version",
* @SWG\Response(
* response=200,
* description="Working"
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
然后运行
php artisan swagger-lume:publish
然后运行
php artisan swagger-lume:generate
这会生成 JSON,可以从您的 localhost:8000 或您为 LUMEN 服务提供服务的任何端口访问
Note: after creating an issue in the repo I found that to access you'll need to serve or run using
php -S localhost:8000 public/index.php
由于
php -S localhost:8000 public
的一些 PHP 路由问题
关于laravel-5.4 - 如何在 Lumen/Laravel 中为 REST API 集成 Swagger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211512/