Composer简单使用

在测试项目时,难免会用到composer

linux下可以直接安装composer命令,其实都一样,本质都是composer.phar文件

安装

Linux下的安装

下载composer.phar文件

1
curl -sS https://getcomposer.org/installer | php

下载完成之后,要把composer.phar文件移动到bin目录里面,方便全局使用composer命令

1
mv composer.phar /usr/local/bin/composer

Windows

直接使用composer.phar文件就行,用的时候

1
php composer.phar xxxxxxx

换源

可以通过下面的命令查看配置

1
php composer.phar config -l -g

image-20220418112156207

全局配置

命令如下

1
php composer.phar config -g repos.packagist composer https://xxxxxxx

国内部分源

镜像名 地址 赞助商 更新频率 备注
阿里云 Composer 镜像 https://mirrors.aliyun.com/composer/ 阿里云 96 秒 推荐
腾讯云 Composer 镜像 https://mirrors.cloud.tencent.com/composer/ 腾讯云 24 小时 -
PHP 国内 Composer 镜像 https://packagist.phpcomposer.com 仁润股份 24 小时 不稳定
华为云 Composer 镜像 https://repo.huaweicloud.com/repository/php/ 华为云 未知 未知
php.cnpkg.org Composer 镜像 https://php.cnpkg.org 安畅网络 60 秒 -

有时候可能会出错,直接换了

image-20220418112439156

当然也可以用如下命令取消配置

1
php composer.phar config -g --unset repos.packagist

项目配置

也可以只修改当前项目的composer配置

在当前项目的composer.json文件中添加

1
2
3
4
5
6
7
8
9
10
11
{
"repositories": [
{
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/" //第一个
},
{
"type": "composer",
"url": "https://packagist.phpcomposer.com" //第二个
},
}

寻找包的过程是先从第一个源中寻找,如果找不到就从第二个源中寻找,这里可以配置多个composer资源库

错误

遇到的错误,此处会持续更新

第一种

1
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 67108864 bytes) in phar

解决方式

  • 可以直接修改php的配置
  • 直接在使用命令时修改
1
php -d memory_limit=-1 composer.phar install

第二种

这是在装一个laravel的时候遇到的

1
2
3
4
5
6
7
8
9
10
Your requirements could not be resolved to an installable set of packages.

Problem 1
- laravel/horizon v4.3.5 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system.
- laravel/horizon 4.x-dev requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system.
- Installation request for laravel/horizon ^4.3.5 -> satisfiable by laravel/horizon[4.x-dev, v4.3.5].

To enable extensions, verify that they are enabled in your .ini files:
- E:\phpstudy\directory\phpstudy_pro\Extensions\php\php7.2.9nts\php.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

解决方式

  • 修改composer.json,找到config
1
2
3
4
5
6
7
8
9
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"platform": {
"ext-pcntl": "7.2",
"ext-posix": "7.2"
}
},

end