标签(空格分隔): phpphp
有同窗反馈写几十个接口文档须要两天的工做量, 随着多部门之间的协做愈来愈频繁, 维护成本愈来愈高, 文档的可维护性愈来愈差, 须要一个工具来管理这些接口的文档, 并可以充当mock server给调用方使用。html
有同窗推荐了swagger+easymock,Swagger是一个简单但功能强大的API表达工具。 这里介绍使用swagger-php生成PHP API文档的方法。nginx
git clone https://github.com/zircote/swagger-php.git composer install
// 全局的 composer global require zircote/swagger-php // 项目中 composer global require zircote/swagger-php
使用 L5 Swagger
https://github.com/DarkaOnLine/L5-Swaggerlaravel
具体安装过程请参考此文档: https://github.com/DarkaOnLin...git
# 建立文件 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) { } }
./swagger-php/bin/openapi demo -o ./docs
API 内容以下:github
# 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'
git clone https://github.com/swagger-api/swagger-ui.git composer install
直接经过Dockerfile构建、启动项目, 或者使用docker-compose进行服务管理。docker
version: '2' services: swagger-ui: build: . ports: - "8080:8080" volumes: - ./dist/:/usr/share/nginx/html/ restart: on-failure
访问 http://localhost:8080/ 便可到 swagger
编辑器预览界面。api
./swagger-php/bin/openapi demo -o ./swagger-ui/dist/
将 api文档输出值swagger ui的根目录下,可经过 http://localhost:8080/openapi.yaml 访问此api文档。restful
执行 Explore
结果如图:composer