yiisoft/yii-swagger
Composer 安装命令:
composer require yiisoft/yii-swagger
包简介
OpenAPI Swagger for Yii Framework
README 文档
README
Yii Swagger
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
Follow updates
yiisoft/yii-swagger 适用场景与选型建议
yiisoft/yii-swagger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 120.84k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2020 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「yii」 「swagger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/yii-swagger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/yii-swagger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/yii-swagger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
A PSR-7 compatible library for making CRUD API endpoints
Priveate for SkeekS CMS
Swagger integration to Laravel 5
Easy Swagger endpoint using the Zend Expressive routes config
一个接口文档生成器
统计信息
- 总下载量: 120.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 23
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-10-02