## 網上搜索巨多的教程,終于找到一篇能夠實現的教程了。
原文地址:[https://blog.csdn.net/qq\_38757174/article/details/116301882](https://blog.csdn.net/qq_38757174/article/details/116301882)
環境:小皮面板 php7x
### 1.新建項目
官網地址:[http://www.hmoore.net/manual/thinkphp6\_0/1037479](http://www.hmoore.net/manual/thinkphp6_0/1037479)
新建項目命令
~~~
composer create-project topthink/think tp
~~~
### 2.安裝依賴
~~~
composer require zircote/swagger-php 3.*
~~~
最新已經到4.\*了,如果不指定版本,會導致后面進行不下去。
安裝成功后

### 3.修改路由
在route文件夾下app.php;
~~~
//下面是新加的
Route::get('/swagger', function() {
$openapi = OpenApi\scan(root_path().'app');
// $openapi = OpenApi\scan('../app');//當然,你也可以用這種相對路徑的寫法,但是我建議還是用上面,避免更換route路徑后出現問題
header('Content-Type: application/json');
echo $openapi->toJson();
});
~~~
如果 報錯??OpenApi\\scan undefined ,那可能就是??swagger 的版本不對,重新安裝到3x的版本即可。
### 4.示例代碼
修改?controller\\Index.php:
~~~
<?php
namespace app\controller;
use app\BaseController;
/**
* @OA\Info(title="thinkphp6接口文檔", version="1.0.1")
*/
class Index extends BaseController
{
/**
* @OA\Get(path="/api/article",
* tags={"文章管理"},
* summary="文章列表",
* @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string", default="123456")),
* @OA\Parameter(name="page", in="query", description="頁碼", @OA\Schema(type="int", default="1")),
* @OA\Parameter(name="limit", in="query", description="行數", @OA\Schema(type="int", default="10")),
* @OA\Response(response="200", description="The User")
* )
*/
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14載初心不改 - 你值得信賴的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">億速云</a> 獨家贊助發布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
/**
* @OA\Post(path="/api/article",
* tags={"文章管理"},
* summary="新增文章",
* @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="multipart/form-data",
* @OA\Schema(
* @OA\Property(description="文章名稱", property="title", type="string", default="dd"),
* @OA\Property(description="文章內容", property="content", type="string"),
* required={"title", "content"})
* )
* ),
* @OA\Response(response="200", description="successful operation")
* )
*/
public function save()
{
//save業務代碼
}
/**
* @OA\Get(path="/api/article/{id}",
* tags={"文章管理"},
* summary="文章詳情",
* @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
* @OA\Parameter(name="id", in="path", description="文章id", @OA\Schema(type="int")),
* @OA\Response(response="200", description="The User")
* )
*/
public function read($id)
{
//read業務代碼
}
/**
* @OA\Put(path="/api/article/{id}",
* tags={"文章管理"},
* summary="編輯文章",
* @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
* @OA\Parameter(name="id", in="path", description="文章id", @OA\Schema(type="int")),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="content-type/json",
* @OA\Schema(
* @OA\Property(description="文章名稱", property="title", type="string"),
* @OA\Property(description="文章內容", property="content", type="string"),
* required={"title", "content"})
* )
* ),
* @OA\Response(response="200", description="successful operation")
* )
*/
public function update($id)
{
//update業務代碼
}
/**
* @OA\Delete(path="/api/article/{id}",
* tags={"文章管理"},
* summary="刪除文章",
* @OA\Parameter(name="token", in="header", description="token", @OA\Schema(type="string")),
* @OA\Parameter(name="id", in="path", description="文章id", @OA\Schema(type="int")),
* @OA\Response(response="200", description="The User")
* )
*/
public function delete($id)
{
//delete業務代碼
}
}
~~~
接下來瀏覽器打開?[http://127.0.0.1/tp/public/index.php/swagger](http://127.0.0.1/tp/public/index.php/swagger)?查看

?這樣就是成功了(因為我們要拿到他的json文件)。
### 5.下載swagger
地址:[https://github.com/swagger-api/swagger-ui/tree/3.x](https://github.com/swagger-api/swagger-ui/tree/3.x)
記得要選3.0的版本

### 6.修改 swagger 文件
將 swagger 里的 dist 里的 index.html 改成下面這樣就好了。

~~~
http://127.0.0.1/tp/public/index.php/swagger
~~~
### 7.最后一步
**修改完成直接打開文件,直接打開文件,直接打開文件**
但是會提示這樣

此時把地址前面修改成 127.0.0.1 即可
~~~
http://127.0.0.1/tp/public/swagger/dist/index.html
~~~

然后我就只會到這一步了。
剩下的自己去研究吧。?
* * *
以上是全部內容了
- thinkphp6執行流程(一)
- php中use關鍵字用法詳解
- Thinkphp6使用騰訊云發送短信步驟
- 路由配置
- Thinkphp6,static靜態資源訪問路徑問題
- ThinkPHP6.0+ 使用Redis 原始用法
- smarty在thinkphp6.0中的最佳實踐
- Thinkphp6.0 搜索器使用方法
- 從已有安裝包(vendor)恢復 composer.json
- tp6with的用法,表間關聯查詢
- thinkphp6.x多對多如何添加中間表限制條件
- thinkphp6 安裝JWT
- 緩存類型
- 請求信息和HTTP頭信息
- 模型事件用法
- 助手函數匯總
- tp6集成Alipay 手機和電腦端支付的方法
- thinkphp6使用jwt
- 6.0session cookie cache
- tp6筆記
- TP6(thinkphp6)隊列與延時隊列
- thinkphp6 command(自定義指令)
- command(自定義指令)
- 本地文件上傳
- 緩存
- 響應
- 公共函數配置
- 七牛云+文件上傳
- thinkphp6:訪問多個redis數據源(thinkphp6.0.5 / php 7.4.9)
- 富文本編輯器wangEditor3
- IP黑名單
- 增刪改查 +文件上傳
- workerman 定時器操作控制器的方法
- 上傳文件到阿里云oss
- 短信或者郵箱驗證碼防刷代碼
- thinkphp6:訪問redis6(thinkphp 6.0.9/php 8.0.14)
- 實現關聯多個id以逗號分開查詢數據
- thinkphp6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 用mpdf生成pdf文件(php 8.1.1 / thinkphp v6.0.10LTS )
- 生成帶logo的二維碼(php 8.1.1 / thinkphp v6.0.10LTS )
- mysql數據庫使用事務(php 8.1.1 / thinkphp v6.0.10LTS)
- 一,創建過濾IP的中間件
- 源碼解析請求流程
- 驗證碼生成
- 權限管理
- 自定義異常類
- 事件監聽event-listene
- 安裝與使用think-addons
- 事件與多應用
- Workerman 基本使用
- 查詢用戶列表按拼音字母排序
- 擴展包合集
- 查詢用戶數據,但是可以通過輸入用戶昵稱來搜索用戶同時還要統計用戶的文章和粉絲數
- 根據圖片的minetype類型獲取文件真實拓展名思路
- 到處excel
- 用imagemagick庫生成縮略圖
- 生成zip壓縮包并下載
- API 多版本控制
- 用redis+lua做限流(php 8.1.1 / thinkphp v6.0.10LTS )
- 【thinkphp6源碼分析三】 APP類之父, 容器Container類
- thinkphp6表單重復提交解決辦法
- 小程序授權
- 最簡單的thinkphp6導出Excel
- 根據訪問設備不同訪問不同模塊
- 服務系統
- 前置/后置中間件
- 給接口api做簽名驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- 6實現郵箱注冊功能的細節和代碼(點擊鏈接激活方式)
- 使用前后端分離的驗證碼(thinkphp 6.0.9/php 8.0.14/vue 3.2.26)
- 前后端分離:用jwt+middleware做用戶登錄驗證(php 8.1.1 / thinkphp v6.0.10LTS )
- vue前后端分離多圖上傳
- thinkphp 分組、頁面跳轉與ajax
- thinkphp6 常用方法文檔
- 手冊里沒有的一些用法
- Swagger 3 API 注釋
- PHP 秒級定時任務
- thinkphp6集成gatewayWorker(workerman)實現實時監聽
- thinkphp6按月新增數據表
- 使用redis 實現消息隊列
- api接口 統一結果返回處理類
- 使用swoole+thinkphp6.0+redis 結合開發的登錄模塊
- 給接口api做簽名驗證
- ThinkPHP6.0 + UniApp 實現小程序的 微信登錄
- ThinkPHP6.0 + Vue + ElementUI + axios 的環境安裝到實現 CURD 操作!
- 異常$e
- 參數請求驗證自定義和異常錯誤自定義