funding字段是composer中用于声明项目资金支持方式的可选配置,位于composer.JSon根层级,格式为包含type和url的对象数组,常见type包括github、patreon、open_collective、tidelift和custom,填写后可在Packagist页面展示赞助入口,帮助开发者获得经济支持,促进开源生态可持续发展。

在 composer.json 文件中,funding 字段用于声明项目的资金支持方式,帮助开发者或维护者获得经济支持。这个字段不会影响代码运行,但对开源生态的可持续发展有重要意义。
什么是 funding 字段?
funding 是 Composer 提供的一个可选字段,允许项目维护者列出可用于资助该项目的平台或链接。当其他开发者使用你的包时,他们可以通过此信息了解如何支持你。
该字段出现在 composer.json 的根层级,格式为一个对象数组,每个对象包含类型(type)和 URL(url)两个键。
funding 字段的填写格式
基本结构如下:
"funding": [ { "type": "github", "url": "https://github.com/sponsors/用户名" }, { "type": "patreon", "url": "https://www.patreon.com/用户名" }, { "type": "open_collective", "url": "https://opencollective.com/项目名" }, { "type": "tidelift", "url": "https://tidelift.com/funding/github/平台名/项目名" }, { "type": "custom", "url": "https://你的网站/donate" } ]
常见 type 值包括:
- github:GitHub Sponsors
- patreon:Patreon 赞助页面
- open_collective:Open Collective 项目页
- tidelift:Tidelift 订阅链接
- custom:自定义捐赠页面,如 PayPal 或微信收款
实际填写示例
假设你在 GitHub 上维护一个开源库,并开通了赞助功能,可以这样写:
{ "name": "your-vendor/your-package", "description": "A sample package", "type": "library", "license": "MIT", "authors": [ { "name": "Your Name", "email": "you@example.com" } ], "require": {}, "funding": [ { "type": "github", "url": "https://github.com/sponsors/your-github-username" }, { "type": "custom", "url": "https://paypal.me/yourpaypal" } ] }
这样,当你发布到 packagist.org 后,页面上会显示“Sponsor”按钮,点击即可跳转到你的赞助链接。
funding 字段的作用与意义
这个字段的主要用途是:
虽然不是必填项,但如果你希望获得社区支持,建议合理填写。
基本上就这些。只要按规范写好 type 和 url,就能让别人更容易支持你的开源工作。不复杂但容易忽略。


