ignatenkovnikita / yii2-swagger
Swagger UI v3 与 yii2 的集成
v1.5
2022-08-01 13:41 UTC
Requires
- bower-asset/swagger-ui: >3.1
- yiisoft/yii2: ^2.0.0
- zircote/swagger-php: >2.0
README
此模块集成了swagger-php和swagger-ui v3。
安装
通过composer安装此扩展是首选方法。
运行
php composer.phar require ignatenkovnikita/yii2-swagger:dev-master
或者将以下内容添加到您的composer.json
文件的require部分:
"ignatenkovnikita/yii2-swagger": "dev-master"
到
使用
扩展安装后,只需在代码中使用它
设置url,其中包含json文件的位置或设置扫描路径
'modules' => [ ... 'swagger' => [ 'class' => \ignatenkovnikita\swagger\Module::class, // 'url' => 'http://petstore.swagger.io/v2/swagger.json', 'path' => '@frontend/modules/api', // disable page with your logic 'isDisable' => function () { return false; }, // replace placeholders in swagger content 'afterRender' => function ($content) { $content = str_replace('{{host}}', 'http://example.com', $content); $content = str_replace('{{basePath}}', '/api/v1', $content); return $content; } ] ... ],
现在您可以在您的域名.com/swagger中打开它并查看API文档
示例模块
/** * Class Module Api * * * @SWG\Swagger( * basePath="{{basePath}}", * host="{{host}}", * schemes={"http"}, * @SWG\Info( * version="1.0", * title="Example API", * @SWG\Contact(name="Example", url="http://example.ru"), * ), * @SWG\Definition( * definition="Error", * required={"code", "message"}, * @SWG\Property( * property="code", * type="integer", * format="int32" * ), * @SWG\Property( * property="message", * type="string" * ) * ) * ) */
示例控制器
/** * @SWG\Post( * path = "/user-device/register", * tags = {"user-device"}, * operationId = "userDevice", * summary = "Регистрация устройства", * description = "Регистрация устройства", * produces = {"application/json"}, * consumes = {"application/json"}, * @SWG\Parameter( * in = "body", * name = "body", * description = "Тело запроса", * required = true, * type = "string", * @SWG\Schema(ref = "#/definitions/UserDeviceForm") * ), * @SWG\Response(response = 200, description = "success") *) * @throws HttpException */
示例表单
/** * @SWG\Definition( * definition="UserDeviceForm", * required={"uuid", "token", "os", "json"}, * @SWG\Property( * property="uuid", * type="string", * description="UUID устройства", * example="e3243" * ), * @SWG\Property( * property="token", * type="string", * description="token для Google Fire Base", * example="e3243" * ), * @SWG\Property( * property="os", * type="string", * description="os устройства android|ios", * example="ios" * ), * @SWG\Property( * property="json", * type="string", * description="json достпные параметры устройства, название модели, версия ОС и др", * example="" * ) * ) */
待办事项
- 添加缓存
- 添加自定义