承接 yiisoft/yii-swagger 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

yiisoft/yii-swagger

最新稳定版本:2.2.0

Composer 安装命令:

composer require yiisoft/yii-swagger

包简介

OpenAPI Swagger for Yii Framework

README 文档

README

Yii

Yii Swagger


Latest Stable Version Total Downloads Build status Code Coverage Mutation testing badge static analysis type-coverage

OpenAPI Swagger for Yii Framework.

Requirements

  • PHP 8.1 - 8.5.

Installation

The package could be installed with Composer:

composer require yiisoft/yii-swagger

Configuration

1. Add route configuration to config/routes.php

use Yiisoft\DataResponse\Middleware\FormatDataResponseAsHtml;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\Yii\Swagger\Action\SwaggerUi;
use Yiisoft\Yii\Swagger\Action\SwaggerJson;

// Swagger routes
Group::create('/swagger', [
    Route::get('')
        ->middleware(FormatDataResponseAsHtml::class)
        ->action(fn (SwaggerUi $swaggerUi) => $swaggerUi->withJsonUrl('/swagger/json-url')),
    Route::get('/json-url')
        ->middleware(FormatDataResponseAsJson::class)
        ->action(SwaggerJson::class),
]),

2. Add attributes to default API controller

use OpenApi\Attributes as OA;

#[OA\Info(title:"My first API", version:"1.0")]
class DefaultController {
    // ...
}

and before actions

use OpenApi\Attributes as OA;

#[OA\Get(
    path: "/api/endpoint",
    summary: "Get default endpoint",
    responses: [
        new OA\Response(response: "200", description: "Get default action"),
    ],
)]
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    // ...
}

See Swagger-PHP documentation for details on how to annotate your code.

3. Configure SwaggerJson action

For attributes to be registered you need to configure SwaggerJson.

You can use the parameters in config/params.php to configure SwaggerJson:

//...
'yiisoft/yii-swagger' => [
    'source-paths' => [
        '@src/Controller' // Directory where annotations are used
    ],
    'cacheTTL' => 60 // Enables caching and sets TTL, "null" value means infinite cache TTL.
],
//...

4. (Optional) Add config for aliases and asset manager

use Yiisoft\Definitions\Reference;
use Yiisoft\Assets\AssetManager;

return [
    //...
    'yiisoft/aliases' => [
        'aliases' => [
            //...
            '@views' => '@root/views',
            '@assets' => '@public/assets',
            '@assetsUrl' => '@baseUrl/assets',
        ],
    ],
    'yiisoft/view' => [
        'basePath' => '@views',
        'defaultParameters' => [
            'assetManager' => Reference::to(AssetManager::class),
        ]
    ],
    //...
];

5. (Optional) Configure SwaggerUi action

You can use the parameters in config/params.php to configure SwaggerUi.

For example, you can enable persisting authorization by setting the persistAuthorization parameter to true.

//...
'yiisoft/yii-swagger' => [
    'ui-params' => [
        'persistAuthorization' => true,
    ],
],
//...

You can find a complete list of parameters by following the link.

6. (Optional) Configure SwaggerService

You can specify options for generation an OpenApi\Annotations\OpenApi instance in config/params.php to configure SwaggerService:

//...
'yiisoft/yii-swagger' => [
    // Default values are specified.
    'options' => [
        'aliases' => [],
        'namespaces' => [],
        'config' => [],
        'validate' => true,
        'version' => OpenApi\Annotations\OpenApi::DEFAULT_VERSION,
    ],
],
//...

Or you can specify definition of OpenApi\Generator in di-web.php:

use Yiisoft\Yii\Swagger\Service\SwaggerService;

return [
    Generator::class => [
        'setAnalyser' => [myAnalyser()],
    ],
    //...
]

For more information about generation an OpenApi\Annotations\OpenApi instance, see the documentation of the zircote/swagger-php package.

Documentation

If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.

License

The Yii Swagger is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

统计信息

  • 总下载量: 114.01k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 31
  • Watchers: 14
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-10-02