在 composer.json 中正确配置 funding 字段需满足:必须为数组,每项含合法 type(如 github、ko_fi)和完整 https url;github 仅显示首个 type 为 github 的赞助链接且需启用 sponsors 功能;packagist 需手动触发更新。

直接在 composer.json 里加 funding 字段就能让 Packagist、GitHub(自动解析)、Composer CLI 等地方显示赞助入口,但字段格式不对或平台不匹配,链接根本不会出现。
composer.json 的 funding 字段必须是数组,且每项含 type 和 url
不是字符串,不是对象,也不是随便写个链接——Packagist 只认严格符合 Schema 的数组结构。常见错误包括:"funding": "https://ko-fi.com/xxx"(无效)、"funding": {"github": "user/repo"}(格式错)、漏掉 type。
-
type必须是 Packagist 官方认可的值,比如github、patreon、opencollective、ko_fi、tidelift、community_bridge等(注意下划线和连字符) -
url必须是完整可访问的 HTTPS 链接,不能是相对路径或短链 - 支持多个赞助渠道,直接写成数组即可,顺序不影响显示
正确示例:
{
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/yourname"
},
{
"type": "ko_fi",
"url": "https://ko-fi.com/yourname"
}
]
}
GitHub 会自动读取 composer.json 的 funding,但只在特定位置显示
GitHub 不是“解析后展示所有链接”,它只在仓库主页右侧边栏的 Sponsor 按钮处使用第一个合法的 github 类型条目;其他类型(如 ko_fi)即使存在也不会出现在这里。
- 如果想让 GitHub 显示自己的 Sponsor 按钮,
type必须是github,且url必须指向https://github.com/sponsors/xxx(不是个人主页或组织页) - GitHub 不识别
patreon或opencollective类型——那些只会对 Packagist 和composer fund命令生效 - 确保仓库已启用 GitHub Sponsors(Settings → Sponsorship),否则按钮不显示,哪怕
funding配置完全正确
composer fund 命令只显示当前项目依赖中声明了 funding 的包
运行 composer fund 后,它列出的是你项目 vendor/ 下所有包的赞助信息,不是你自己的项目——除非你用 composer fund vendor/package-name 指定某一个。
- 你的项目本身不会出现在
composer fund默认列表里,因为 Composer 只扫描已安装的依赖,不扫描当前工作目录 - 想测试自己项目的配置是否生效,得先
composer require your-vendor/your-package:dev-main(本地开发时用 path repo 或 symlink 更方便) -
type值影响 CLI 显示图标:比如github显示 Octocat,ko_fi显示 K,拼写错误会导致图标 fallback 为问号
最容易被忽略的一点:Packagist 同步有延迟,改完 composer.json 后要手动触发更新(在 Packagist 页面点 “Update”),或者等下次 webhook 自动抓取——改完就以为立刻生效,其实可能卡在缓存里几个小时。










