sadi01/yii2-swagger

Swagger UI v3 与 yii2 集成

1.0.1 2022-11-14 10:15 UTC

This package is auto-updated.

Last update: 2024-09-14 15:34:54 UTC


README

本模块集成了 swagger-phpswagger-ui v3

Latest Stable Version Total Downloads Latest Unstable Version License

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require sadi01/yii2-swagger:dev-master

或添加

"sadi01/yii2-swagger": "dev-master"

到您的 composer.json 文件的 require 部分。

用法

扩展安装后,只需在您的代码中简单使用它即可

设置 URL,其中包含 json 文件的位置或设置扫描路径

'modules' => [
      ...
      'swagger' => [
            'class' => \sadi01\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;
            }
       ]
        ...
    ],

现在您可以通过访问 yourdomain.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=""
 *      )
 * )
 */

待办事项

  • 添加缓存
  • 添加自定义

捐赠

Donate