如何安装?

官网地址:Composer (getcomposer.org),目前个人看到的最新版为 2.4.4。

可以先尝试安装官方的介绍一步步操作:

Command-line installation

To quickly install Composer in the current directory, run the following script in your terminal. To automate the installation, use the guide on installing Composer programmatically.

注意:链接对应了类 Unix系统的安装手段。
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
unlink('composer-setup.php'); } echo PHP_EOL;"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; 
php composer-setup.php
php -r "unlink('composer-setup.php');"

个人在笔记 [[【PHP】PHP开发必备配置 - Windows]] 中提到过这个工具的安装,这里不再重复介绍。

Composer 命令

Composer 命令的常用命令如下:

查看Composer版本

composer --versin

案例:

C:\Users\adong>composer --version
Composer version 2.4.4 2022-10-27 14:39:29

升级将默认升到最新版本

composer self-update
友情提示: 版本升级命令将会从官方版本去下载composer,phar镜像,速度将受限!强烈建议全局配置加速镜像

Composer 自我诊断

使用Composer 进行自我诊断的代码如下:

composer diagnose

参考案例如下:

C:\Users\adong>composer diagnose
Checking platform settings: OK
Checking git settings: OK git version 2.37.0
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking HTTP proxy: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: 57815BA2 7E54DC31 7ECC7CC5 573090D0  87719BA6 8F3BB723 4E5D42D0 84A14642
Dev Public Key Fingerprint: 4AC45767 E5EC2265 2F0C1167 CBBB8A2B  0C708369 153E328C AD90147D AFE50952
OK
Checking composer version: OK
Composer version: 2.4.4
PHP version: 7.4.33
PHP binary path: E:\Program Files\php74\php.exe
OpenSSL version: OpenSSL 1.1.1s  1 Nov 2022
cURL version: 7.70.0 libz 1.2.12 ssl OpenSSL/1.1.1s
zip: extension present, unzip not available, 7-Zip not available

清楚缓存

清楚缓存的命令如下

composer clear

具体案例如下:

C:\Users\adong>composer clear
Cache directory does not exist (cache-vcs-dir):
Clearing cache (cache-repo-dir): C:\Users\adong\AppData\Local\Composer\repo
Clearing cache (cache-files-dir): C:\Users\adong\AppData\Local\Composer\files
Clearing cache (cache-dir): C:\Users\adong\AppData\Local\Composer
All caches cleared.

项目更新 composer.lock

composer update --lock

个人测试案例如下:

C:\Users\adong>composer update --lock
Composer could not find a composer.json file in C:\Users\adong
To initialize a project, please create a composer.json file. See https://getcomposer.org/basic-usage

调试

Composer调试只需要在命令前加上-vvv就可以打印出错信息

例如下载laravel命名为blog的项目

composer -vvv create-project laravel/laravel blog

安装依赖包

安装依赖包需要在指定项目的根路径执行下面的操作:

composer install

更新依赖包

该命令仅限在项目根目录操作

更新所有依赖包

composer update

更新指定依赖包

composer update monolog/monolog

更新多个指定依赖包

composer update monolog/monolog symfony/dependency-injection

根据通配符匹配更新依赖包

composer update monolog/monolog symfony/*

移除依赖包

移除指定依赖包

composer remove monolog/monolog

根据通配符匹配移除依赖包

composer remove monolog/monolog symfony/*

搜索依赖包

搜索依赖包:输出包及其描述信息

composer search monolog

使用案例如下:

C:\Users\adong>composer search monolog
monolog/monolog                       Sends your logs to files, sockets, inboxes, databases and various web services
symfony/monolog-bundle                Symfony MonologBundle
symfony/monolog-bridge                Provides integration for Monolog with various Symfony components
symfony/debug-bundle                  Provides a tight integration of the Symfony VarDumper component and the Serv...
maxbanton/cwh                         AWS CloudWatch Handler for Monolog library
easycorp/easy-log-handler             ! Abandoned ! A handler for Monolog that optimizes log me...
theorchard/monolog-cascade            Monolog extension to configure multiple loggers in the blink of an eye and a...
bramus/monolog-colored-line-formatter Colored Line Formatter for Monolog
wazaari/monolog-mysql                 A handler for Monolog that sends messages to MySQL
logentries/logentries-monolog-handler A handler for Monolog that sends messages to Logentries.com.
inpsyde/logzio-monolog                Logz.io integration for Monolog
newrelic/monolog-enricher             Monolog components to enable New Relic Logs
cmdisp/monolog-microsoft-teams        Monolog Handler for sending messages to Microsoft Teams using the Incoming W...
inpsyde/wonolog                       Monolog-based logging package for WordPress.
amphp/log                             Non-blocking logging for PHP based on Amp and Monolog.

只搜索包,可以添加 --only-name选项,如下所示

composer search --only-name monolog

执行命令的返回结果较多:

根据通配符搜索依赖包

composer search mono*

案例如下:

C:\Users\adong>composer  search mono*
mead-steve/mono-snag                         Bugsnag integration for monolog. An abstract handler that sends messages to Bugsnag
digikraaft/mono-php                          PHP Implementation of Mono API
codeat3/blade-mono-icons                     A package to easily make use of Mono Icons in your Laravel Blade views.
yzen.dev/mono-processor                      This Processor will display in the logs bread crumbs by which you can more quickly and accurately identify the cause of the error.
remdex/livehelperchat                        Live Helper Chat - live support for your website. Featuring web and desktop clients. Compatible Windows, Linux, Mac. Desktop client powered by QT. Supports mobile phones based on XMPP se...
aedart/athenaeum                             Athenaeum is a mono repository; a collection of various PHP packages
druidfi/mona-plugin                          Mona Composer Plugin for Drupal 7 sites
dbp/relay-mono-connector-payunity-bundle     A PayUnity connector for the mono bundle for the Relay API gateway
dbp/relay-mono-connector-generic-bundle      A generic connector for the mono payment bundle for the Relay API gateway
dbp/relay-mono-connector-campusonline-bundle A CO payment connector for the mono bundle for the Relay API gateway
dbp/relay-mono-bundle                        A payment bundle for the Relay API gateway
timitao/monoa
loicjazon/mono
druidfi/mona                                 ! Abandoned ! Example setup using mona-plugin
vldmr-k/mono-acquiring                       Monobank Acquiring library

查看依赖包

该命令仅限项目根目录使用

查看当前项目已安装的依赖包

composer show

根据通配符当前项目进行筛选

composer show laravel*

查看当前项目指定依赖包

composer show laravel/tinker 

Xander
198 声望51 粉丝