haohetao / yii2-swagger
swagger与yii2集成
2.1.0
2019-06-14 15:11 UTC
Requires
- bower-asset/swagger-ui: *
- yiisoft/yii2: ^2.0.0
- zircote/swagger-php: *
This package is auto-updated.
Last update: 2024-09-28 00:41:18 UTC
README
Yii2 Swagger 扩展
swagger-php与yii2集成。
安装
安装此扩展的首选方式是通过composer。
运行以下命令:
php composer.phar require --prefer-dist haohetao/yii2-swagger "dev-master" --dev
或者将以下内容添加到您的composer.json
文件的require部分:
"haohetao/yii2-swagger": "dev-master"
```
使用方法
配置两个操作如下:
public function actions()
{
return [
//The document preview addesss:http://api.yourhost.com/site/doc
'doc' => [
'class' => 'haohetao\swagger\SwaggerAction',
'restUrl' => \yii\helpers\Url::to(['/site/api'], true),
],
//The resultUrl action.
'api' => [
'class' => 'haohetao\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缓存。
最后
如果您还有疑问,可以参考示例。