Swagger 生成 PHP API 接口文档

Lumen微服务生成Swagger文档


1、概况

有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文档的可维护性越来越差, 需要一个工具来管理这些接口的文档, 并能够充当mock server给调用方使用。

有同学推荐了swagger+easymock,Swagger是一个简单但功能强大的API表达工具。 这里介绍使用swagger-php生成PHP API文档的方法。

2、安装与使用

2.1 安装swagger-php包


git clone https://github.com/zircote/swagger-php.git composer install

// 全局的
composer global require zircote/swagger-php // 项目中
composer global require zircote/swagger-php

2.2 laravel项目安装

使用 L5 Swagger https://github.com/DarkaOnLine/L5-Swagger

具体安装过程请参考此文档: https://github.com/DarkaOnLin...

2.3 编写API注解

# 创建文件 demo/customer.php
<?php /**
* @OA\Info(title="My First API", version="0.1")
*/
class Customer
{
/**
* @OA\Get(
* path="/customer/info",
* summary="用户的个人信息",
* description="这不是个api接口,这个返回一个页面",
* @OA\Parameter(name="userId", in="query", @OA\Schema(type="string"), required=true, description="用户ID"),
* @OA\Response(
* response="200",
* description="An example resource"
* )
* )
*/
public function info(int $userId, string $userToken)
{ }
}

2.4 生成swagger API 文件


./swagger-php/bin/openapi demo -o ./docs

API 内容如下:


# docs/openapi.yaml
openapi: 3.0.0
info:
title: 'My First API'
version: '0.1'
paths:
/customer/info:
get:
summary: 用户的个人信息
description: '这不是个api接口,这个返回一个页面'
operationId: 'Customer::info'
parameters:
-
name: userId
in: query
description: 用户ID
required: true
schema:
type: string
responses:
'200':
description: 'An example resource'

3、展示


git clone https://github.com/swagger-api/swagger-ui.git composer install

直接通过Dockerfile构建、启动项目, 或者使用docker-compose进行服务管理。


version: '2' services:
swagger-ui:
build: .
ports:
- "8080:8080"
volumes:
- ./dist/:/usr/share/nginx/html/
restart: on-failure

访问 http://localhost:8080/ 即可到 swagger 编辑器预览界面。


./swagger-php/bin/openapi demo -o ./swagger-ui/dist/

将 api文档输出值swagger ui的根目录下,可通过 http://localhost:8080/openapi.yaml 访问此api文档。

执行 Explore 结果如图:

4、参考资料

原文地址:https://segmentfault.com/a/1190000016735909

Swagger 生成 PHP API 接口文档的更多相关文章

  1. 整合swagger2生成Restful Api接口文档

    整合swagger2生成Restful Api接口文档 swagger Restful文档生成工具 2017-9-30 官方地址:https://swagger.io/docs/specificati ...

  2. Asp.Net Core2.0 WebAPI 使用Swagger生成漂亮的接口文档

    1.引用NuGet: Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerGen 或 <PackageReference I ...

  3. 自动生成web api接口文档

    然后打开web程序,访问ip:port/Help. 为什么可以直接输入Help就能访问呢,因为这个插件本身已经配置了路径,如下. public class HelpPageAreaRegistrati ...

  4. spring boot使用swagger生成api接口文档

    前言 在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成swagger接口文档 具体可以查看本站spring boot ...

  5. Swagger解决你手写API接口文档的痛

    首先,老规矩,我们在接触新事物的时候, 要对之前学习和了解过的东西做一个总结. 01 痛     苦 不做.不行 之前,前后端分离的系统由前端和后端不同的编写,我们苦逼的后端工程师会把自己已经写完的A ...

  6. SpringBoot + Swagger2 自动生成API接口文档

    spring-boot作为当前最为流行的Java web开发脚手架,相信越来越多的开发者会使用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端 ...

  7. Api接口文档管理工具,你知道哪些呢?

    上周看到有人在我的Github开源项目中提了个issue,说是否考虑接入swagger.那今天我就用swagger与其他接口文档工具做对比,同时说说Api接口文档工具的那点事.如今,在前后端分离开发的 ...

  8. 构建标准OpenStack API接口文档

    1.构建API接口文档标准参考: http://docs.openstack.org/contributor-guide/api-guides.html 2.构建API接口文档步骤参考下面的Patch ...

  9. api(接口)文档管理工具

    api(接口)文档管理工具 欢迎光临:博之阅API管理平台  ,做为一个app开发者,还没有用到api管理工具,你就OUT了 点击进入:程序员精华博客大全  

随机推荐

  1. CF 689D - Friends and Subsequences

    689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...

  2. Python 爬歌曲

    Python 爬歌曲 小练习 import re import time import requests # http://www.htqyy,com/top/hot # http://f2.htqy ...

  3. hadoop 使用java操作hdfs

    1.创建目录 import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.ha ...

  4. C#调用带结构体指针的C Dll的方法【转】

    发现一篇文章关于C#调用DALL动态链接库的函数的,复制下来学习用.感谢作者的分析,原文传送门:https://www.cnblogs.com/ye-ming/p/8004314.html 在C#中调 ...

  5. C 语言中函数的跳转

    1.同一个函数内,可以使用goto语句: eg: void  text_1( void ) { char i=0; a : i++; printf ( " text_1 = %d \r\n& ...

  6. mybatis中sql标签和include标签

    1.首先定义一个sql标签,一定要定义唯一id.(name,age是要查询的字段) <sql id="Base_Column_List" >name,age</s ...

  7. linux svn命令具体解释

    检測是否安装svn:svnserve --version svn服务的关闭:killall svnserve 创建svn库:svnadmin create /opt/svn/repos 配置自己主动启 ...

  8. [HTML5] Inlining images with SVG and data URIs

    The main reason you want to do I"nlining images with SVG and data URIs" is to reduce http ...

  9. 实战c++中的vector系列--再谈vector的insert()方法(都是make_move_iterator惹的祸)

    之前说过了关于vector的insert()方法,把vector B的元素插入到vector A中.vector A中的结果我们可想而知,可是vector B中的元素还会怎样? 看看之前写过的程序: ...

  10. Codeforces Round #274 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/479 这次自己又仅仅能做出4道题来. A题:Expression 水题. 枚举六种情况求最大值就可以. 代码例如以下: #inc ...