php使用swagger来实现api文档展示和测试

安装node

cd /usr/local/
wget https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.xz
yum -y install xz
xz -d node-v10.15.0-linux-x64.tar.xz
tar -xvf node-v10.15.0-linux-x64.tar
mv node-v10.15.0-linux-x64 node
ln -s /usr/local/node/bin/node /usr/bin/node
ln -s /usr/local/node/bin/npm /usr/bin/npm

安装swagger-gui

npm install -g http-server
ln -s /usr/local/node/bin/http-server /usr/bin/http-server
yum -y install git
git clone https://github.com/swagger-api/swagger-ui
cd swagger-ui
http-server// -p 80(默认端口8080)
http://127.0.0.1:8080

安装swagger-php

{
  "require": {
    "zircote/swagger-php": "^3.0.1"
  },
  "autoload": {
  },
  "repositories": {
    "packagist": {
      "type": "composer",
      "url": "https://packagist.phpcomposer.com"
    }
  }
}

composer install

写php注解

<?php

/**
 * @OA\OpenApi(
 *    @OA\Info(
 *      version="1.0.0",
 *      title="api文档",
 *      description="api文档"
 *    ),
 *    @OA\Server(
 *       description="测试环境",
 *       url="127.0.0.1",
 *   ),
 *    @OA\Server(
 *       description="正式环境",
 *       url="10.0.0.1",
 *   )
 * )
 *
 * @OA\Schema(
 *     schema="news",
 *     type="object",
 *     required={"title","content"},
 *     @OA\Property(
 *         property="id",
 *         type="integer",
 *         description="编号"
 *     ),
 *     @OA\Property(
 *         property="title",
 *         type="string",
 *         description="标题"
 *     ),
 *     @OA\Property(
 *         property="content",
 *         type="string",
 *         description="正文"
 *     ),
 *     @OA\Property(
 *         property="cover",
 *         type="string",
 *         description="配图地址"
 *     ),
 *     @OA\Property(
 *         property="time",
 *         type="string",
 *         description="发布时间"
 *     )
 * )
 *
 * @OA\Schema(
 *     schema="Paging",
 *     @OA\Property(
 *          property="page",
 *          type="integer",
 *          description="页码",
 *          format="int32",
 *          default="1"
 *     ),
 *     @OA\Property(
 *          property="limit",
 *          type="integer",
 *          description="每页个数",
 *          format="int32",
 *          minimum="0",
 *          exclusiveMinimum=true,
 *          maximum="100",
 *          exclusiveMaximum=false
 *     )
 * )
 */
class NewsController
{

    /**
     * @OA\Get(
     *     path="/news/{type}",
     *     summary="获取资讯",
     *     description="返回包含已发布的资讯列表",
     *     @OA\Parameter(
     *          name="type",
     *          in="path",
     *          required=true,
     *          description="类型",
     *          @OA\Schema(
     *             type="string",
     *             enum={"entertainment","Sports"}
     *         )
     *     ),
     *     @OA\Parameter(
     *          name="page",
     *          in="query",
     *          @OA\Schema(
     *             @OA\Items(ref="#/components/schemas/Paging")
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="一个资讯列表",
     *         @OA\Schema(
     *             type="array",
     *             @OA\Items(ref="#/components/schemas/news")
     *         ),
     *     )
     * )
     */
    public function indexAction()
    {
        echo 'a';
    }
}

生成文档

./vendor/bin/openapi -o api.yml -e vendor --format yaml --debug .

 

转载于:https://my.oschina.net/u/232595/blog/2998779

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值