通过composer进行安装,命令:
composer require zircote/swagger-php:2.*
代码需要引入
use Swagger\Annotations as SWG;
默认必须都要加上:
/**
* @SWG\Swagger(
* @SWG\Info(
* title="测试接口",
* version="1.0.0"
* )
* )
*/
post请求加Authorization认证:
/**
* @SWG\Post(
* path="/api/xxxx",
* tags={"xxxx"},
* summary="xxxxx",
* @SWG\Parameter(
* in="header",
* name="Authorization",
* description="token",
* required=true,
* type="string"
* ),
* @SWG\SecurityScheme(
* securityDefinition="api_key",
* type="apiKey",
* in="header",
* description="token",
* name="Authorization"
* ),
* @SWG\Response(
* response=200,
* description="http状态码",
* @SWG\Schema(
* @SWG\Property(
* property="code",
* type="number",
* description="状态码"
* ),
* @SWG\Property(
* property="msg",
* type="string",
* description="错误提示"
* ),
* @SWG\Property(
* property="data",
* type="array",
* items={},
* description="返回的数据"
* )
* )
* )
*
* )
*/
post请求:
h="/api/xxxxx",
* tags={"xxxxxx"},
* summary="用户登录",
* produces={"application/json"},
* @SWG\Parameter(
* in="formData",
* name="name",
* description="账号",
* type="string",
* required = true,
* ),
* @SWG\Parameter(
* in="formData",
* name="password",
* description="密码",
* type="string",
* required = true,
* ),
* @SWG\Response(
* response=200,
* description="http状态码",
* @SWG\Schema(
* @SWG\Property(
* property="code",
* type="number",
* description="状态码"
* ),
* @SWG\Property(
* property="msg",
* type="string",
* description="错误提示"
* ),
* @SWG\Property(
* property="data",
* type="array",
* items={},
* description="返回的数据"
* )
* )
* )
*
* )
*/
Post Json请求:
/**
* @SWG\Post(
* path="/api/xxxx",
* tags={"xxxx"},
* summary="xxxxxxx",
* produces={"application/json"},
* @SWG\Parameter(
* in="body",
* required=true,
* name="body",
* description="请求参数",
* @SWG\Schema(
* @SWG\Property(
* property="name",
* type="string",
* description="账号"
* ),
* @SWG\Property(
* property="password",
* type="string",
* description="密码"
* ),
* @SWG\Property(
* property="role_ids",
* type="array",
* items={1},
* description="角色ID数组元素"
* ),
* )
* ),
* @SWG\Response(
* response=200,
* description="http状态码",
* @SWG\Schema(
* @SWG\Property(
* property="code",
* type="number",
* description="状态码"
* ),
* @SWG\Property(
* property="msg",
* type="string",
* description="错误提示"
* ),
* @SWG\Property(
* property="data",
* type="array",
* items={},
* description="返回的数据"
* )
* )
* )
*
* )
*/
生存json文件执行代码:
php vendor\zircote\swagger-php\bin\swagger 代码路径 -o 存放路径
还需要下载swagger-ui,然后修改dist目录index.html 里面url路径