farit-slv/yii2-oas3

Yii2 Open Api Swagger 3集成

维护者

详细信息

github.com/FaritSlv/yii2-oas3

源代码

安装次数: 55

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 11

类型:yii2-extension

v0.4 2023-07-12 07:42 UTC

This package is auto-updated.

Last update: 2024-09-12 10:07:41 UTC


README

要求

  • PHP 7.4
  • Yii2框架

安装

安装此包装器首选方式是通过composer

php composer.phar require farit-slv/yii2-oas3 "*"

或者

composer require farit-slv/yii2-oas3 "*"

或者添加到composer.json文件的require部分

"farit-slv/yii2-oas3" : "*"

集成

向Web控制器添加操作(例如SiteController.php)

public function actions()
{
    return [
        'api-redoc' => [
            'class' => 'farit-slv\swagger\ViewAction',
            'apiJsonUrl' => \yii\helpers\Url::to(['/site/api-json'], true),
            'useReDoc' => true,
        ],
        'api-docs' => [
            'class' => 'farit-slv\swagger\ViewAction',
            'apiJsonUrl' => \yii\helpers\Url::to(['/site/api-json'], true),
        ],
        'api-json' => [
            'class' => 'farit-slv\swagger\JsonAction',
            'dirs' => [
                Yii::getAlias('@api/modules/api/controllers'),
                Yii::getAlias('@api/modules/api/models'),
                Yii::getAlias('@api/models'),
            ],
        ],
    ];
}

Open Api Swagger 3示例注解

API服务器描述

/**
 * @OA\Info(
 *   version="1.0",
 *   title="Application API",
 *   description="Server - Mobile app API",
 *   @OA\Contact(
 *     name="John Smith",
 *     email="john@example.com",
 *   ),
 * ),
 * @OA\Server(
 *   url="https://example.com/api",
 *   description="main server",
 * )
 * @OA\Server(
 *   url="https://dev.example.com/api",
 *   description="dev server",
 * )
 */
 
class DefaultController extends Controller
{
...

控制器注解

/**
 * @OA\Get(path="/",
 *   summary="Handshake",
 *   tags={"handshake"},
 *   @OA\Parameter(
 *     name="access-token",
 *     in="header",
 *     required=false,
 *     @OA\Schema(
 *       type="string"
 *     )
 *   ),
 *   @OA\Response(
 *     response=200,
 *     description="Returns Hello object",
 *     @OA\MediaType(
 *         mediaType="application/json",
 *         @OA\Schema(ref="#/components/schemas/Hello"),
 *     ),
 *   ),
 * )
 */
public function actionIndex()
{
... 

模型注解

/**
 *@OA\Schema(
 *  schema="Hello",
 *  @OA\Property(
 *     property="message",
 *     type="string",
 *     description="Text message"
 *  ),
 *  @OA\Property(
 *     property="time",
 *     type="integer",
 *     description="Server current Unix time"
 *  ),
 *  @OA\Property(
 *     property="date",
 *     type="string",
 *     format="date-time",
 *     description="Server current date time"
 *  )
 *)
 */
class Hello extends Model
{
...

捐赠

授权协议

此curl包装器遵循MIT授权协议