iomanager / swgenerator
生成Swagger OpenApi文档的Json文件
dev-main
2024-03-07 12:50 UTC
This package is auto-updated.
Last update: 2024-09-07 13:56:56 UTC
README
composer require iomanager/swgenerator:dev-main
关于LaravelOpenApiManager 作者:Ignacio Sebastian Olivieri 首版 27/02/2024
为了制作最佳文档以供团队工作,我认为从27/02/2024开始创建一个应用,用于快速创建Laravel每个控制器上的注解,以与Swagger一起使用。
需求
使用此包必须遵守规则
- 使用正确的控制器和模型名称,此代码基于此逻辑。
- 示例1 控制器(ProductsController)模型(Product)OK
- 示例2 控制器(ProdController)模型(Products)BAD!
- 您可以使用控制器中的资源路由和个人路由,仅限于:dir routes/api.php OK
必填项
- 步骤1 创建带方法的控制器
- 步骤2 创建迁移表
- 步骤3 创建模型
- 步骤4 创建带有方法的路由或使用Routes::resource或Routes::apiResource
- 步骤5 在类Controller extends BaseController中定义OpenApi的路径
/**
* @OA\Get(
* path="https://:8000/docs/api-docs.json",
* @OA\Response(
* response="200",
* description="The data"
* )
* )
*/
class ProductsController extends Controller
{
...
步骤6 创建Artisan命令
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Iomanager\Swgenerator\SwagController;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Process;
use L5Swagger\Http\Controllers\SwaggerController;
use function Laravel\Prompts\info;
class CreateSwagl5 extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:create-swagl5';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This command create the Annotations Open Api 3.0 on all Controllers from API routes';
/**
* Execute the console command.
*/
public function handle()
{
$swag = new SwagController;
$res = $swag->generateAnnotations();
if ($res==="OK"){
$this->info('The json file was generated!');
}else{
$this->info('JSON NOT created!') ;
}
}
}
步骤7 在config/filesystem.php的磁盘上添加
'storage' => [
'driver' => 'local',
'root' => storage_path('api-docs'),
'throw' => false,
],
- 在您的Controller中创建所有方法之后,您可以运行以下命令
- Artisan命令:要向您的控制器添加注解,您需要使用此命令,这将为每个Api方法生成注解
php artisan app:create-swagl5
-
此文档和代码目前正在建设中,目前可以工作,但需要更多参数,这将由我完成;)。
-
必填项:从URL传递的参数如下
-
路由
-
Route::post('/product/{id}/{price}', [ProductsController::class,'myMethod']);
-
这些{id}/{price}参数是数据库表的字段,因此您需要写出与表反射相等的名称
-
Route::post('/product/{id}/{price}', [ProductsController::class,'myMethod']); OK!
-
|--表
-
|-------- id
-
|-------- name
-
|-------- price
-
|-------- description
-
Route::post('/product/{id_prod}/{price_product}', [ProductsController::class,'myMethod']); BAD!
-
|--表
-
|-------- id
-
|-------- name
-
|-------- price
-
|-------- description
个人路由中的额外参数
- 示例路由:这不是资源路由,而是带有个人参数的新路由,该路由指定了控制器中您感兴趣的新方法
- 参数{id_prod}对应于下一个参数的第一个类型...
Route::post('/add/product/{id_prod}/{price}/{extra}', [ProductsController::class,'myMethod2']);