1. 安装前端
git clone https://github.com/swagger-api/swagger-ui.git
http://localhost/yii2/swagger-ui/dist/index.html 打开URL就可以看到前端界面了(内容还是默认的)
之后找到dist目录, 打开index.html把其中的那一串url改成自己的, 比如http://localhost/yii2/swagger-docs/swagger.json(文件是通过下面的步骤生成的
2. 安装后端
composer require zircote/swagger-php
php ./vendor/zircote/swagger-php/bin/swagger 需要生成接口文档的mvc目录 -o 接口文档生成位置
注意:老版本是/vendor/zircote/swagger-php/bin/swagger
新版本是/vendor/zircote/swagger-php/bin/openapi
"-o" 前面代表API源目录, 即你想要生成哪个目录的API文档, 你的项目代码目录. "-o" 后面是生成到哪个path
目标目录会产生一个swagger.json 或 openapi.yaml 文件,这个文件放到前端dist/index.html中可以访问的地方即可()
然后再看http://localhost/yii2/swagger-ui/dist/index.html, 生成了API文档
准备工作都做好了, 那就写代码注释就行了, 注释怎么写? 参考官方文档http://zircote.com/swagger-php/annotations.html
---------------------------------------------------------------------------------------------------------------------------------
可能出现的问题:
1、Could not open input file: composer.phar
解决:php -r "readfile('https://getcomposer.org/installer');" | php