PHPackages                             bestyii/yii2-openapi-reader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. bestyii/yii2-openapi-reader

ActiveYii2-extension[API Development](/categories/api)

bestyii/yii2-openapi-reader
===========================

A reader for php swagger annotation

2.0.1(2y ago)53.5k↓33.3%2[2 issues](https://github.com/bestyii/yii2-openapi-reader/issues)BSD-2-ClausePHP

Since Feb 27Pushed 2y ago2 watchersCompare

[ Source](https://github.com/bestyii/yii2-openapi-reader)[ Packagist](https://packagist.org/packages/bestyii/yii2-openapi-reader)[ RSS](/packages/bestyii-yii2-openapi-reader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (0)

Yii2 OpenApi Specification Reader 模块
====================================

[](#yii2-openapi-specification-reader-模块)

原理是采用php的注释来写api文档，注释的语法采用php annotation方式进行解析。 解析后符合OpenAPI Specification 规范，可以通过 swagger UI 或 Redoc 进行渲染成可读性强带有交互的api文档。

[![Latest Stable Version](https://camo.githubusercontent.com/006bc112446e6a10f5c5a0df7af531b2f33aa55aaa6f20f96b19afb1cfa52c7c/68747470733a2f2f706f7365722e707567782e6f72672f626573747969692f796969322d6f70656e6170692d7265616465722f762f737461626c65)](https://packagist.org/packages/bestyii/yii2-openapi-reader)[![Total Downloads](https://camo.githubusercontent.com/236a0dc8d6f669cde3245ec2a8c369502cdbd7a0d9db67a7d2976e08b5338268/68747470733a2f2f706f7365722e707567782e6f72672f626573747969692f796969322d6f70656e6170692d7265616465722f646f776e6c6f616473)](https://packagist.org/packages/bestyii/yii2-openapi-reader)[![License](https://camo.githubusercontent.com/b953d9f3596d6a3a712d682e98ecf981d595a8fea6a2b14188aad96b4b02d145/68747470733a2f2f706f7365722e707567782e6f72672f626573747969692f796969322d6f70656e6170692d7265616465722f6c6963656e7365)](https://packagist.org/packages/bestyii/yii2-openapi-reader)

swagger UI

[![alt swagger UI](https://camo.githubusercontent.com/d67c98b09f90b6b464c39b8f5223c9adfe6d0b902ba9ac988590e8001d31dfea/68747470733a2f2f737461746963312e736d617274626561722e636f2f737761676765722f6d656469612f696d616765732f746f6f6c732f6f70656e736f757263652f737761676765725f75692e706e673f6578743d2e706e67 "swagger UI")](https://camo.githubusercontent.com/d67c98b09f90b6b464c39b8f5223c9adfe6d0b902ba9ac988590e8001d31dfea/68747470733a2f2f737461746963312e736d617274626561722e636f2f737761676765722f6d656469612f696d616765732f746f6f6c732f6f70656e736f757263652f737761676765725f75692e706e673f6578743d2e706e67)

Redoc：

[![alt redoc](https://raw.githubusercontent.com/Redocly/redoc/master/demo/redoc-demo.png "redoc")](https://raw.githubusercontent.com/Redocly/redoc/master/demo/redoc-demo.png)

这个模块集成了：

- [swagger-php](https://github.com/zircote/swagger-php)
- [swagger-ui v3](https://github.com/swagger-api/swagger-ui)
- [Redocly/redoc](https://github.com/Redocly/redoc)

安装 Installation
---------------

[](#安装-installation)

通过 [composer](http://getcomposer.org/download/)安装.

项目中直接运行

```
php composer.phar require bestyii/yii2-openapi-reader:dev-master

```

或者添加下面代码到 `composer.json`文件

```
"bestyii/yii2-openapi-reader": "dev-master"

```

使用 Usage
--------

[](#使用-usage)

Once the extension is installed, simply use it in your code by :

You set url, where locate json file OR set path for scan

```
if (YII_ENV_DEV) {
 $config['modules']['openapireader'] = [
        'class' => \bestyii\openapiReader\Module::class,
        'defaultDoc' => 'api',
        'path' => [
            'api' => '@grazio/api',
            'extensions' => '@app/extensions',
        ],
        // disable page with your logic
        'isDisable' => function () {
            return false;
        },
        // replace placeholders in swagger content
        'afterRender' => function ($content) {
            $content = str_replace('{{HOST}}', \yii\helpers\Url::base(true), $content);
            $content = str_replace('{{BASE_PATH}}', '/api', $content);
            $content = str_replace('{{SERVER_DESCRIPTION}}', 'description', $content);
            return $content;
        }
    ];
}
```

现在就可以访问你的API文档了

```
# swagger 风格
http://yoururl.com/openapireader

# redoc 风格
http://yoururl.com/openapireader/default/redoc

```

示例 Module

```
/**
 * @OA\OpenApi(
 *     @OA\Info(
 *         version="0.0.1",
 *         title="OpenApi",
 *         description="This is a sample server Petstore server.  You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the Bearer `access token` to test the authorization filters.",
 *     ),
 *     @OA\Server(
 *         description="Test",
 *         url="http://api.bestyii.com/api/"
 *     ),
 *     @OA\Server(
 *         description="Prod",
 *         url="http://api.bestyii.com/v2/"
 *     ),
 *     @OA\ExternalDocumentation(
 *         description="更多关于达卡拉的信息",
 *         url="http://bestyii.com"
 *     )
 * )
 */

/**
 * @OA\SecurityScheme(
 *   securityScheme="bearerAuth",
 *   type="http",
 *   scheme="bearer",
 *   in="header",
 *   bearerFormat="JWT"
 * )
 * https://swagger.io/docs/specification/authentication/basic-authentication/
 */
```

示例 controller

```
