Composer 介绍
Composer 是 PHP 用来管理依赖关系的工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。
Composer 将这样为你解决问题:
a) 你有一个项目依赖于若干个库。
b) 其中一些库依赖于其他库。
c) 你声明你所依赖的东西。
d) Composer 会找出哪个版本的包需要安装,并安装它们(将它们下载到你的项目中)。
准备工作
- github账号(https://github.com/)
- packagist账号(https://packagist.org/)
开始搭建
1. 创建github仓库
在github上创建仓库,并将代码克隆到本地
$ git clone https://github.com/lypeng/composer_test.git
2. 创建 Composer 配置文件
进入项目根目录,创建Composer配置文件composer.json,手动创建或者使用composer init
命令创建,文件内容大致如下:
{
"name": "lypeng/composer_test",
"description": "Third party message push",
"authors": [
{
"name": "作者",
"email": "邮箱"
}
],
"license": "MIT",
"require": {
"php": ">=5.4"
},
"autoload": {
"psr-4": {
"lypeng\\composertest\\": "src/"
}
}
}
3. 提交代码到 Github
根据功能编写代码,并把测试完成没有问题的代码提交到Github。
4. 发布包到Packagist
登录 Packagist,检出github仓库代码。
a) 点击submit
按钮
b) Repository URL (Git/Svn/Hg) 项填写github仓库地址
c) 检出
系统会根据仓库中 composer.json 文件自动设置包的相关信息。
5. 版本设置
使用 git 的 tag 标签
git tag的部分命令:
git tag <tagName> #创建本地标签
git push origin <tagName> #推送标签到远程仓库
git push origin --tags #推送所有标签到远程仓库
git show <tagName> #查看本地某个标签的详细信息
git tag #查看本地所有标签
git tag -l #查看本地所有标签
git ls-remote --tags origin 查看远程所有标签
git tag -d <tagName> #删除本地标签
git push origin :<tagName> #删除远程标签
最后推荐几款好用的composer包
包名 | 描述 |
---|---|
overtrue/wechat | easywechat(好用的微信开发SDK) |
topthink/think-queue | TP5消息队列 |
phpoffice/phpspreadsheet | excel、pdf、csv |
spatie/laravel-backup | laravel数据备份 |
fengqi/hanzi | 汉字转拼音 |
bacon/bacon-qr-code | 二维码 |
piwik/device-detector | 获取设备品牌版本信息 |
intervention/image | 网页转图片 |