ThinkPHP5.1 Swagger-PHP
官网
https://xiaoxiami.gitbook.io/swagger/swagger-php-wen-dang
https://www.kancloud.cn/punghunger/apidoc/934753
https://github.com/zircote/swagger-php
https://www.kancloud.cn/chengguang/swagger-php/1172137
1、在项目根目录下载Swagger-PHP
composer require zircote/swagger-php

2、配置Swagger的php入口
- 第一种方法 在路由中写
- Route::get('/swagger', function() {
$openapi = \OpenApi\Generator::scan(root_path().'app'); //这个是Swagger 读取的目录
//$openapi = \OpenApi\Generator::scan('../app');//当然,你也可以用这种相对路径的写法,但是我建议还是用上面,避免更换route路径后出现问题
header('Content-Type: application/json');
echo $openapi->toJson();
}); ![]()
- Route::get('/swagger', function() {
- 第二种方法 写一个方法为入口
![]()
-
public function hello()
{
$openapi = \OpenApi\Generator::scan([\think\Container::get('app')->getAppPath() . '/index/']);
header('Content-Type: application/x-yaml');
return $openapi->toYaml();}
-
接口Swagger 写法
-
/**
* 账号密码登录
* @OA\Post(
* path="/index/index/index",
* tags={"总后台-登录"},
* @OA\Response(description="code:200,登录成功、code:1002,错误",response="200"),
* @OA\RequestBody(description="post数据",
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* @OA\Property(
* property="userName",
* description="用户名",
* type="string"
* ),
* @OA\Property(
* property="password",
* description="密码",
* type="string"
* )
* )
* ),
* )
* )
* @return \think\response\Json
* @throws \ReflectionException
* @throws \app\common\library\exception\Exception
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index()
{// $bb = new Hello();
// echo $bb->hello();die;
echo $this->Hello->hello();die;
error_log('aa = ' . var_dump($aa,1));
} -
![]()
-
- Swagger 前端入口
- 然后 通过php 访问 index.html 文件 出现下图就对了
![]()
3、接口注释另一种写法
- 在类上面引入 Swagger 写法 例如







浙公网安备 33010602011771号