avatar

mdo

Hello

  • 首页
  • 知识库
  • 归档
  • 标签
  • 关于
主页 Thinkphp8.1项目集成swagger
文章

Thinkphp8.1项目集成swagger

发表于 2025-10-31 更新于 2025-10- 31
作者 mdo
6~8 分钟 阅读

注意:swagger-php 5.x 默认只支持 PHP 属性注解,不再推荐/默认支持 PHPDoc 注释!

1、 使用composer安装swagger-php

`composer require zircote/swagger-php` 

2、 验证安装:

`./vendor/bin/openapi --version` 

3、 在控制器目录controller下新建类Swagger.php:

`<?php

namespace appcontroller;
use OpenApiAttributes as OA;

#[OAInfo(
    title: "THINKPHP8 API文档",
    version: "1.0.1",
    description: "THINKPHP8项目接口文档",
    contact: new OAContact(
        name: "THINKPHP8",
        email: "support@xxx.com",
        url: "https://xxx.com"
    )
)]
#[OATag(
    name: "Index",
    description: "首页相关接口"
)]
#[OATag(
    name: "Merchant",
    description: "商户相关接口"
)]
class Swagger
{

}` 

![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

4、 在需要扫描的控制器类(Merchant.php)中加入注解:

`<?php
namespace appcontroller;
use OpenApiAttributes as OA;
class Merchant extends BaseApp
{
#[OAPost(
        path: "/api/merchant/register",
        summary: "商户注册",
        tags: ["Merchant"],
        requestBody: new OARequestBody(
            required: true,
            content: new OAJsonContent(
                required: ["email", "password", "timestamp", "sign"],
                properties: [
                    new OAProperty(property: "email", type: "string", description: "邮箱"),
                    new OAProperty(property: "password", type: "string", description: "密码"),
                    new OAProperty(property: "timestamp", type: "integer", description: "时间戳"),
                    new OAProperty(property: "sign", type: "string", description: "签名"),
                ]
            )
        ),
        responses: [
            new OAResponse(
                response: 200,
                description: "注册成功"
            ),
            new OAResponse(
                response: 400,
                description: "参数错误或签名失败"
            )
        ]
    )]
public function register()
    {
    }
}` 

![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

5、 运行扫描命令,自动扫描并生成public目录下的swagger.json文件:

`./vendor/bin/openapi --output ./public/swagger.json ./app/controller` 

6、 下载swagger-ui,以便可以可视化呈现接口文档:
下载最新版本的swagger-ui文件,通常是dist.zip压缩包,解压后复制到项目public目录下,并修改swagger-initializer.js文件,将资源指向swagger.json

`window.ui = SwaggerUIBundle({
    url: "/swagger.json",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  });` 

![](https://csdnimg.cn/release/blogv2/dist/pc/img/runCode/icon-arrowwhite.png)

7、 运行或部署项目后,可访问127.0.0.1/swagger-ui/#/,即可看到接口文档。

知识库
Thinkphp
许可协议:  CC BY 4.0
分享

相关文章

7月 21, 2026

think-orm 2.0.62 单独设置数据表字段缓存驱动和数据缓存驱动

think-cache 拥有强大的多通道(Multi-store)管理能力,但问题的根源在于 think-orm 底层的调用机制太死板。即使您在 think-cache 中配置了 file 和 redis 两个完全独立的通道,think-orm 默认也只会向您通过 Db::setCache() 注入

7月 1, 2026

WebSocket Server + 独立 RPC Server

从架构角度来说,它已经接近 think-swoole 能做到的极限了,但是我仍然不建议继续这样维护。 原因不是代码写法,而是 think-swoole 本身的事件模型。 第一处问题:addListener() 仍然共享 Worker 你的代码: $rpcServer = $server->addLi

6月 25, 2026

Linux | Reqable · API抓包调试 + API测试一站式工具

Reqable是什么? Reqable = Fiddler + Charles + Postman 极简的设计、极高的性能、丰富的功能、桌面手机多端平台。 多协议流量分析 基于经典的MITM中间人代理方案捕获和分析您的应用流量,自适应HTTP/HTTPS/SOCKS4/SOCKS5等多种代理协议,并

下一篇

PHP使用swagger

上一篇

日报完全自动化的

最近更新

  • 完美地解决 TP3 老系统数据的平滑读取
  • thinkphp3 redis序列化和反序列化
  • Table 空间极易发生哈希冲突并溢出
  • 将监控程序直接跑在云端
  • AI 驱动型 Facebook 群组关键词监控 Chrome 浏览器插件

热门标签

API CodeGeex Gitkraken Management Manticore Premiere Sublime Swoole ThinkPHP ThinkPHP5

目录

©2026 mdo. 保留部分权利。