php框架YII2的swagger踩坑记录

2024-05-24 17:38
文章标签 php 记录 框架 swagger yii2

本文主要是介绍php框架YII2的swagger踩坑记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1. light/yii2-swagger 使用的YII框架这个包。(能不能拉下来,就得看你了,反正我是拉了很久)(默认是swagger3.0,与下面的demo不符合,下面的demo是用的2.0)由于我使用的工具只支持2.0,所以用2.0了,3.0变成了openapi规范了。

  2. 开始可以先试用一下文档下面的demo

搭成功后, 他有个demo.gif,用cli启动命令,然后

然后在浏览器中输入:http://127.0.0.1:8080 就可以访问demo里面的接口对应的文档了。

(如果页面显示你要auth验证的话,你就把SiteController第58行.api_key 为 test 注释掉就可以了 )

    3.我主要是想要生成json文件导入到其他工具使用。(其他方式需要在代码中配置)

在项目根目录

php vendor/zircote/swagger-php/bin/swagger ./modules/v1/controllers/ -o ./
第一个路径是执行文件,第二个路径是你写了注释的PHP文件,第三个是你生成swagerr.json的路径

生成后,大概是长成这样。

{"swagger": "2.0","paths": {"/users": {"get": {"tags": ["user"],"summary": "\u83b7\u53d6\u7528\u6237\u5217\u8868","description": "\u6d4b\u8bd5\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2aarray","produces": ["application/json"],"parameters": [{"name": "access_token","in": "query","description": "access token6666666666","required": true,"type": "string"}],"responses": {"200": {"description": " success"}}},"post": {"tags": ["user"],"summary": "\u521b\u5efa\u7528\u6237\u63a5\u53e3","description": "\u6d4b\u8bd5Param\u662f *query* \u7c7b\u578b, \u5982\u679c\u8bbe\u7f6e\u6210 *formData* \u7c7b\u578b\u7684\u5c31\u53ef\u4ee5\u4f7f\u7528post\u83b7\u53d6\u6570\u636e","produces": ["application/json"],"parameters": [{"name": "username","in": "formData","description": "\u7528\u6237\u59d3\u540d","required": true,"type": "string"},{"name": "phone","in": "formData","description": "\u624b\u673a\u53f7","required": true,"type": "string"},{"name": "sex","in": "formData","description": "\u6027\u522b 1. \u7537 2.\u5973 \u6b64\u9879\u4e3a\u975e\u5fc5\u586b\u9879.\u5c55\u793a\u6210select","required": false,"type": "integer","enum": [1,2]}],"responses": {"200": {"description": " success"},"401": {"description": "\u9700\u8981\u91cd\u65b0\u767b\u9646","schema": {"$ref": "#/definitions/Error"}}}},"options": {"tags": ["user"],"summary": "options","operationId": "userOptions","consumes": ["application/json"],"produces": ["application/json"],"responses": {"200": {"description": "success","headers": {"Allow": {"type": "GET, POST, HEAD, OPTIONS"},"Content-Type": {"type": "application/json; charset=UTF-8"}}}}}},"/users/{id}": {"put": {"tags": ["user"],"summary": "\u66f4\u65b0\u7528\u6237\u63a5\u53e3","description": "*path*\u7c7b\u578b\u7684\u53c2\u6570\u4f1a\u653e\u5165\u8bf7\u6c42\u5730\u5740\u5730\u5740\u4e2d","produces": ["application/json"],"parameters": [{"name": "id","in": "path","description": "\u7528\u6237ID","required": true,"type": "integer"},{"name": "username","in": "formData","description": "\u7528\u6237\u59d3\u540d","required": true,"type": "string"},{"name": "phone","in": "formData","description": "\u624b\u673a\u53f7","required": true,"type": "string"},{"name": "sex","in": "formData","description": "\u6027\u522b 1. \u7537 2.\u5973 \u6b64\u9879\u4e3a\u975e\u5fc5\u586b\u9879.\u8fd9\u91cc\u6307\u5b9a\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u9879\uff0c\u90a3\u4e48\u4f1a\u9ed8\u8ba4\u9009\u4e2d","required": false,"type": "integer","default": 1,"enum": [1,2]},{"name": "job","in": "formData","description": "\u8fd9\u91cc\u53ef\u4ee5\u8bbe\u7f6e\u9ed8\u8ba4\u503c","required": false,"type": "string","default": "\u7a0b\u5e8f\u733f"},{"name": "avatar","in": "formData","description": "\u7c7b\u578b\u8bbe\u7f6e\u4e3a`file`\u5219\u53ef\u4ee5\u5c55\u793a\u4e0a\u4f20\u6309\u94ae\uff0c\u5728\u540e\u7aef\u548c\u666e\u901a\u4e0a\u4f20\u4e00\u6837\u5904\u7406","required": false,"type": "file"}],"responses": {"200": {"description": " success"},"401": {"description": "\u9700\u8981\u91cd\u65b0\u767b\u9646","schema": {"$ref": "#/definitions/Error"}}}}},"/users/query": {"post": {"tags": ["user"],"summary": "\u5220\u9664\u7528\u6237","description": "\u8fd9\u91cc\u5c55\u793a\u4e00\u4e2a\u7a0d\u5fae\u9ad8\u7aef\u70b9\u7684\u7528\u6cd5","operationId": "QueryUser","consumes": ["application/json"],"produces": ["application/json"],"parameters": [{"name": "body","in": "body","description": "\u8fd9\u91cc\u8981\u4e3b\u8981\u914d\u7f6e`request`\u7ec4\u4ef6\u7684parsers\uff0c\u6765\u652f\u6301\u63a5\u6536\u8fd9\u4e2a\u7c7b\u578b\u7684\u8bf7\u6c42","required": true,"schema": {"$ref": "#/definitions/UserIdList"},"type": "string"}],"responses": {"200": {"description": "success"}}}}},"definitions": {}
}

 swagger3.0的生成文件长这样

openapi: 3.0.0
paths:/pets:get:description: 'Returns all pets from the system that the user has access to'operationId: findPetsparameters:-name: tagsin: querydescription: 'tags to filter by'required: falsestyle: formschema:type: arrayitems:type: string-name: limitin: querydescription: 'maximum number of results to return'required: falseschema:type: integerformat: int32responses:'200':description: 'pet response'content:text/xml:schema:type: arrayitems:$ref: '#/components/schemas/Pet'text/html:schema:type: arrayitems:$ref: '#/components/schemas/Pet'application/json:schema:type: arrayitems:$ref: '#/components/schemas/Pet'application/xml:schema:type: arrayitems:$ref: '#/components/schemas/Pet'default:description: 'unexpected error'content:text/xml:schema:$ref: '#/components/schemas/ErrorModel'text/html:schema:$ref: '#/components/schemas/ErrorModel'application/json:schema:$ref: '#/components/schemas/ErrorModel'application/xml:schema:$ref: '#/components/schemas/ErrorModel'

 

可以参考的其他人和一些资料链接:

https://www.jianshu.com/p/554cd3762ab1

https://www.cnblogs.com/jcydd/p/11455989.html

https://openapi-map.apihandyman.io/

https://huangwenchao.gitbooks.io/swagger/content/

 

 

PHP的资料是真的少。。。。插件也是。。。。

暂时更新到这个里吧。

 

 

这篇关于php框架YII2的swagger踩坑记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999082

相关文章

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

SpringBoot3集成swagger文档的使用方法

《SpringBoot3集成swagger文档的使用方法》本文介绍了Swagger的诞生背景、主要功能以及如何在SpringBoot3中集成Swagger文档,Swagger可以帮助自动生成API文档... 目录一、前言1. API 文档自动生成2. 交互式 API 测试3. API 设计和开发协作二、使用

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6

python与QT联合的详细步骤记录

《python与QT联合的详细步骤记录》:本文主要介绍python与QT联合的详细步骤,文章还展示了如何在Python中调用QT的.ui文件来实现GUI界面,并介绍了多窗口的应用,文中通过代码介绍... 目录一、文章简介二、安装pyqt5三、GUI页面设计四、python的使用python文件创建pytho

MyBatis框架实现一个简单的数据查询操作

《MyBatis框架实现一个简单的数据查询操作》本文介绍了MyBatis框架下进行数据查询操作的详细步骤,括创建实体类、编写SQL标签、配置Mapper、开启驼峰命名映射以及执行SQL语句等,感兴趣的... 基于在前面几章我们已经学习了对MyBATis进行环境配置,并利用SqlSessionFactory核

PHP执行php.exe -v命令报错的解决方案

《PHP执行php.exe-v命令报错的解决方案》:本文主要介绍PHP执行php.exe-v命令报错的解决方案,文中通过图文讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录执行phpandroid.exe -v命令报错解决方案执行php.exe -v命令报错-PHP War

cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个?

跨平台系列 cross-plateform 跨平台应用程序-01-概览 cross-plateform 跨平台应用程序-02-有哪些主流技术栈? cross-plateform 跨平台应用程序-03-如果只选择一个框架,应该选择哪一个? cross-plateform 跨平台应用程序-04-React Native 介绍 cross-plateform 跨平台应用程序-05-Flutte

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

Spring框架5 - 容器的扩展功能 (ApplicationContext)

private static ApplicationContext applicationContext;static {applicationContext = new ClassPathXmlApplicationContext("bean.xml");} BeanFactory的功能扩展类ApplicationContext进行深度的分析。ApplicationConext与 BeanF