yii2mod/yii2-swagger
Composer 安装命令:
composer require yii2mod/yii2-swagger
包简介
Swagger Documentation Generator for Yii2 Framework
README 文档
README
Swagger Documentation Generator for Yii2 Framework
Swagger/OpenAPI Documentation Generator for Yii2 Framework.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2mod/yii2-swagger "*"
or add
"yii2mod/yii2-swagger": "*"
to the require section of your composer.json.
Configuration
You need to configure two actions as follows:
<?php namespace app\controllers; use Yii; use yii\helpers\Url; use yii\web\Controller; /** * @SWG\Swagger( * basePath="/", * produces={"application/json"}, * consumes={"application/x-www-form-urlencoded"}, * @SWG\Info(version="1.0", title="Simple API"), * ) */ class SiteController extends Controller { /** * @inheritdoc */ public function actions(): array { return [ 'docs' => [ 'class' => 'yii2mod\swagger\SwaggerUIRenderer', 'restUrl' => Url::to(['site/json-schema']), ], 'json-schema' => [ 'class' => 'yii2mod\swagger\OpenAPIRenderer', // Тhe list of directories that contains the swagger annotations. 'scanDir' => [ Yii::getAlias('@app/controllers'), Yii::getAlias('@app/models'), ], ], 'error' => [ 'class' => 'yii\web\ErrorAction', ], ]; } }
Usage
- Creating a Controller
First, create a controller class app\controllers\UserController as follows:
namespace app\controllers; use app\models\User; use yii\data\ActiveDataProvider; use yii\rest\Controller; /** * Class UserController */ class UserController extends Controller { /** * @SWG\Get(path="/user", * tags={"User"}, * summary="Retrieves the collection of User resources.", * @SWG\Response( * response = 200, * description = "User collection response", * @SWG\Schema(ref = "#/definitions/User") * ), * ) */ public function actionIndex() { $dataProvider = new ActiveDataProvider([ 'query' => User::find(), ]); return $dataProvider; } }
- Creating
Userdefinition
You need to create folder app/models/definitions and add User definition class as follows:
<?php namespace app\models\definitions; /** * @SWG\Definition(required={"username", "email"}) * * @SWG\Property(property="id", type="integer") * @SWG\Property(property="email", type="string") * @SWG\Property(property="username", type="string") */ class User { }
- Configuring URL Rules
Then, modify the configuration of the urlManager component in your application configuration:
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ 'GET,HEAD users' => 'user/index', ], ]
- Enabling JSON Input
To let the API accept input data in JSON format, configure the parsers property of the request application component to use the yii\web\JsonParser for JSON input:
'request' => [ 'parsers' => [ 'application/json' => 'yii\web\JsonParser', ] ]
Trying it Out
Now you can access to swagger documentation section through the following URL:
http://localhost/path/to/index.php?r=site/docs
View in the browser
Support us
Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.
yii2mod/yii2-swagger 适用场景与选型建议
yii2mod/yii2-swagger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 403.53k 次下载、GitHub Stars 达 63, 最近一次更新时间为 2017 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yii2」 「yii2 swagger」 「yii2 api documentation generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii2mod/yii2-swagger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii2mod/yii2-swagger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii2mod/yii2-swagger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
A PSR-7 compatible library for making CRUD API endpoints
Swagger integration to Laravel 5
Easy Swagger endpoint using the Zend Expressive routes config
一个接口文档生成器
OpenAPI specification generator and Swagger UI for PHP 7.2-8.4 projects (standalone and Nette Framework)
统计信息
- 总下载量: 403.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 63
- 点击次数: 13
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-14