Composer簡易教程
1. composer安裝
參考官方教程:https://docs.phpcomposer.com/00-intro.html
2. composer安裝第三方包
1. 命令行安裝:
composer require "curl/curl"
2. 編輯composer.json文件安裝:
{
"name": "test/test",
"description": "",
"license": "MIT",
"authors": [
{
"name": "a",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.2.0",
"curl/curl": "^1.6" //這是需要安裝的包
}
"config": {
// 不要.git文件夾
"preferred-install": "dist"
}
}
執行composer install/update安裝
3. 自己製作composer安裝包
1. 創建包
自己在github/gitlab上創建了一個項目地址是:https://github.com/test/test
此項目的compoesr.json文件中需要定義名字,詳細內容如下:
{
// 注意,這裏定義的名字,和之後被require的時候用到的名字相同,而不是github url的後綴
"name": "test1/test1",
"authors": [
{
"name": "a",
"email": "[email protected]"
}
],
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true,
"secure-http": false
}
}
2. require該包
在需要使用該包的項目中創建composer.json,內容如下:
{
"name": "aaa/bbb",
"authors": [
{
"name": "a",
"email": "[email protected]"
}
],
"require": {
"test1/test1": "dev-master"
},
"repositories": [
{
"type": "vcs",
// 這個地址填該項目的連接地址
"url": "https://github.com/test/test"
}
],
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true,
"secure-http": false
}
}
3. 安裝該包
執行`composer install`就可以安裝自己的包了,而不是從packagist上安裝
4. 自己搭建composer倉庫
參考:https://github.com/composer/satis
5. install和update的區別
兩者都可以安裝包,區別有:
`install`會讀取composer.lock文件,根據緩存的指定版本進行安裝,如果沒有,則會在安裝完畢後創建;
`update`不會讀取composer.lock文件,而是升級某個包或者所有包,並更新composer.lock文件
所以對於生產環境,推薦使用`composer install`