gish94 /
swagger 与 yii2 的集成
1.0.6.1
2017-12-14 10:20 UTC
Requires
- bower-asset/swagger-ui: ^3.4.5
- yiisoft/yii2: ^2.0.0
- zircote/swagger-php: ^2.0
README
Yii2 Swagger 扩展
这是从 lichunqiang/yii2-swagger 分支出来的,支持 swagger-ui 3.4.5。
swagger-php 与 yii2 的集成。
集成 swagger-ui 与 swagger-php。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require --prefer-dist light/yii2-swagger "~1.0.4" --dev
或者在您的 composer.json 文件的 require 部分添加以下内容:
"light/yii2-swagger": "~1.0.4"
使用方法
配置以下两个操作
public function actions()
{
return [
//The document preview addesss:http://api.yourhost.com/site/doc
'doc' => [
'class' => 'light\swagger\SwaggerAction',
'restUrl' => \yii\helpers\Url::to(['/site/api'], true),
],
//The resultUrl action.
'api' => [
'class' => 'light\swagger\SwaggerApiAction',
//The scan directories, you should use real path there.
'scanDir' => [
Yii::getAlias('@api/modules/v1/swagger'),
Yii::getAlias('@api/modules/v1/controllers'),
Yii::getAlias('@api/modules/v1/models'),
Yii::getAlias('@api/models'),
],
//The security key
'api_key' => 'balbalbal',
],
];
}
为了安全,您可以配置 API 密钥以进行保护。
缓存
public function actions()
{
return [
// ...
'api' => [
// ...
'cache' => 'cache',
'cacheKey' => 'api-swagger-cache', // default is 'api-swagger-cache'
],
];
}
清除缓存
访问清除缓存 URL YOUR_API_URL?clear-cache 或 YOUR_API_URL?api_key=YOUR_API_KEY&clear-cache
示例: curl 'https:///v1/swagger/api?clear-cache'
您将看到: 成功清除 swagger api 缓存。