swagger生成php接口文檔

1. 安裝前端

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

swagger-ui下載

下載之後找到dist目錄, 打開index.html把其中的那一串url改成自己的, 比如http://localhost/yii2/swagger-docs/swagger.json

然後打開URL就可以看到前端界面了, 應該是沒內容的, 因爲還沒生成swagger.json, 生成好之後你設置的URL就起了作用, 直接訪問前端就好

http://localhost/yii2/swagger-ui/dist/index.html

2. 安裝後端

php composer.phar require zircote/swagger-php
php /Users/DC/www/yii2/vendor/zircote/swagger-php/bin/swagger /Users/DC/www/yii2/vendor/zircote/swagger-php/Examples -o /Users/DC/www/yii2/swagger-docs

"-o" 前面代表API源目錄, 即你想要生成哪個目錄的API文檔, 你的項目代碼目錄. "-o" 後面是生成到哪個path

目標目錄會產生一個swagger.json的文件,這個文件放到前端dist/index.html中可以訪問的地方即可

 

然後再看http://localhost/yii2/swagger-ui/dist/index.html, 生成了API文檔

準備工作都做好了, 那就寫代碼註釋就行了, 註釋怎麼寫? 參考官方文檔http://zircote.com/swagger-php/annotations.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章