原创

从0到1教你在tp6中集成swagger-php来自动生成文档


写文档是程序员恼火的事情,如果能自动生成文档那就好了,这里介绍了swagger-php 自动生成文档,废话不多说,我们直接开始。网上的一些教程都是老掉牙的,非常影响我们的实际操作,建议搭建看我这篇教程。

大致步骤如下
1:安装swagger-php
2:安装swagger-ui
3:配置接口地址,添加备注
4:修改ui文件

好了我们来详细讲解一下吧。
1:安装swagger-php

  1. composer require zircote/swagger-php

这里会安装最新的3.*版本

2:安装ui文件

分享链接:http://pan.micuer.com/#s/7DKEbijw
访问密码:micuer.com

3:添加备注

  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\PileOrder;
  5. /**
  6. * @OA\Info(title="My First API", version="0.1")
  7. */
  8. /**
  9. * @OA\Get(
  10. * path="/api/resource.json",
  11. * @OA\Response(response="200", description="An example resource")
  12. * )
  13. */
  14. class ChargePile extends Api
  15. {
  16. protected $noNeedLogin = ['*'];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 根据uid或unionid,查询用户预约充电桩的状态列表
  20. * @desc 根据uid或unionid,查询用户预约充电桩的状态列表,暂不用此函数
  21. * @param string uid
  22. * @param string unionid
  23. * @return array|string
  24. */
  25. /**
  26. * @OA\Get(
  27. * path="/queryCarList",
  28. * summary="根据uid或unionid,查询用户预约充电桩的状态列表",
  29. * @OA\Response(
  30. * response=200,
  31. * description="根据uid或unionid,查询用户预约充电桩的状态列表"
  32. * ),
  33. * @OA\Response(
  34. * response="default",
  35. * description="an ""unexpected"" error"
  36. * )
  37. * )
  38. */
  39. public function queryCarList(){
  40. $uid = $this->request->param("uid","","trim,intval");
  41. $unionid = $this->request->param("unionid","","trim");
  42. $mod = new PileOrder();
  43. if(!$uid && !$unionid){
  44. return json(["ret"=>304,"msg"=>"参数错误","data"=>[]]);
  45. }
  46. if($uid) {
  47. $list = $mod
  48. ->where("uid",$uid)
  49. ->order("t_update_time DESC")
  50. ->select();
  51. }
  52. if($unionid){
  53. $list = $mod
  54. ->where("unionid",$unionid)
  55. ->order("t_update_time DESC")
  56. ->select();
  57. }
  58. foreach ($list as $k => $v){
  59. $list[$k]["mobile"] =substr_replace($v["mobile"], '****', 3, 4);
  60. }
  61. return json(["ret"=>200,"msg"=>"成功","data"=>$list]);
  62. }
  63. }

4:修改UI文件

5:接口实现方法

  1. public function index()
  2. {
  3. $openapi = \OpenApi\Generator::scan(['../application/api/controller']);
  4. header('Content-Type: application/x-yaml');
  5. echo $openapi->toYaml();
  6. }

注解方式参考:
https://www.sdk.cn/details/9pPQD6wqK09L8ozvNy

附:如果是命令行生成文档对应的文件

  1. php ./vendor/bin/openapi ./app/controller -o ./public

注意-o前面的文件夹和后面的文件夹路径

在线视频教程
https://micuu.com/new/1650.html

附:一个简单的备注示例

  1. /**
  2. * @OA\Post (
  3. * path="/api/xxxx/createOrder",
  4. * summary="创建xxx",
  5. * description="根据用户uid或unionid,来创建预约安装充电桩。如果已经有记录(不管是否已安装还是未安装),都提示“已预约”",
  6. * @OA\Parameter(name="uid",in="query",required=false,description="用户id"),
  7. * @OA\Parameter(name="unionid",in="query",required=false,description="unionid"),
  8. * @OA\Parameter(name="name",in="query",required=false,description="用户的name"),
  9. * @OA\Parameter(name="n_realcar_id",in="query",required=false,description="车辆id"),
  10. * @OA\Parameter(name="car_series",in="query",required=false,description="车系"),
  11. * @OA\Parameter(name="vin_end_six",in="query",required=false,description="车架号后6位"),
  12. * @OA\Parameter(name="vin",in="query",required=false,description="车架号vin码"),
  13. * @OA\Parameter(name="mobile",in="query",required=false,description="手机号"),
  14. * @OA\Parameter(name="province",in="query",required=false,description="省"),
  15. * @OA\Parameter(name="city",in="query",required=false,description="市"),
  16. * @OA\Parameter(name="address",in="query",required=false,description="详细地址"),
  17. * @OA\Response(
  18. * response=200,
  19. * description="ture或者false"
  20. * )
  21. * )
  22. */
留言反馈
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈