定制 fgh151/yii2-swagger 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fgh151/yii2-swagger

最新稳定版本:0.0.10

Composer 安装命令:

composer require fgh151/yii2-swagger

包简介

yii2 swagger files generator

README 文档

README

Yii2 swagger generator

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist fgh151/yii2-swagger "*"

or add

"fgh151/yii2-swagger": "*"

to the require section of your composer.json file.

Usage

  1. Add module in config and set dirs with annotations
'modules' => [
    'swagger' => [
        'class' => fgh151\swagger\Module::class,
        'sources' => [
            dirname(__DIR__).'/controllers', //here can be aliases, like '@app/controllers'
            dirname(__DIR__).'/models',
        ],
        'schema' => '/api/schema', //optional depend on route config
    ],
],
  1. Add routes
'rules' => [
    '/swagger/doc.json' => 'swagger/swagger/doc',
    '/swagger/ui' => 'swagger/swagger/ui',
],
  1. Add controller. It can be not useful and has random name, but contain annotations. Example:
<?php

namespace app\controllers;

use OpenApi\Attributes\Info;
use OpenApi\Attributes\OpenApi;
use OpenApi\Attributes\Server;
use yii\web\Controller;

#[OpenApi(
    info: new Info(version: '1.0.0', title: 'Super API title'),
)]
#[Server(url: 'https://api.example.com', description: 'Super API description')]
class SwaggerController extends Controller
{
}
  1. Add annotations to controllers. Example:
class SomeController extends \yii\web\Controller {
    #[Get(path: '/magic', summary: 'Magic API method.')]
    public function someAction() {
       //Magic here
    }
}
  1. Add annotations to models. Example:
#[Schema(title: 'MyModel', description: 'Magic mode', properties: [
    new Property(property: 'Id', description: 'Идентификатор', type: 'integer'),
])]
class MyModel extends ActiveRecord
{
}
  1. Add annotations to DTO. Example:
#[Schema(title: 'MyModel', description: 'Magic DTO')]
class MyModel extends ActiveRecord
{
    #[\OpenApi\Attributes\Property(title: 'Some attribute name')]
    public string $attributeName = '';
}

See annotations

Cache

To use cache for generated swagger json specify module parameters

'modules' => [
        'swagger' => [
            'class' => \fgh151\swagger\Module::class,
            'enableCache' => true,
            'cacheDuration' => 3600, //Optional, default 3600
            'cache' => 'cache', //Cache component, string|CacheInterface, optional, default 'cache'
        ],
    ]

统计信息

  • 总下载量: 207
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2024-05-07