How to install and use Composer

03-13-2023

This article mainly explains how to install and use Composer. The content of the explanation in this article is simple and clear, easy to learn and understand. Please follow the editor's ideas and slowly deepen, let's research and learn how to install and use Composer together!


Installation

  • Linux

Composer installation is relatively simple, It is not recommended to use management packages such as yum to install directly, because your default php version may be modified. We can skip installing with:

curl -sS getcomposer.org/installer | php

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

  • Windows

Download and run Composer-Setup.exe, it will install the latest version of Composer, it is recommended to add the environment variable of the system, so that you can directly use the composer command in any directory.

Modify the mirror source

The default mirror source domestic download is relatively slow, it is recommended to replace the mirror source

composer config -g repo.packagist composer packagist.phpcomposer .com

You can also use Ali’s mirror source (recommended)

composer config -g repo.packagist composer mirrors.aliyun.com/composer/< /p>

config -g/--global means global configuration

Composer common commands

  • init initialization

Parameters: --name: The name of the package. --description: Description of the package. --author: The author of the package. --homepage: The homepage of the package. --require: Other packages that need to depend must have a version constraint.and should follow the format foo/bar:1.0.0 . --require-dev: The dependency package of the development version, the content format is the same as --require. --stability (-s): The value of the minimum-stability field. Copy code

  • require add

require command adds a new dependency package to the current directory composer.json file and can specify a version.

composer require vendor/package:2.* vendor/package2:dev-master

install

The install command reads the composer.json file from the current directory, handles the dependencies, and installs it into the vendor directory.

If there is a composer.lock file in the current directory, it will read the dependency version from this file instead of obtaining dependencies from the composer.json file. This ensures that every consumer of the library gets the same version of dependencies.

If there is no composer.lock file, composer will create it after processing dependencies.

You can simply understand composer.lock as the cache of the extension library.

update

In order to get the latest version of the dependencies and update the composer.lock file, you should use the update command.

composer update

This will resolve all dependencies of the project and write the exact version number to composer.lock.

If you just want to update a few packages, you can list them individually like this:

composer update vendor/package vendor/package2

==Note==: The update command will update the latest dependencies of the limited version in composer.json. For example, if you write "monolog/monolog": "1.*", the update command may update 1.0 to version 1.2, but not 1.0 updated to 2.0. So you need to pay attention when using it.

search

The search command allows you to search for dependent packages for the current project, usually it only searches packages on packagist.org, you can simply enter your search criteria.

show

To list all available packages, you can use the show command.

1.jpg

dump-autoload

Print the autoload index, in some cases you need to update the autoloader, for example in your package A new class has been added. You can use dump-autoload to do it without having to execute install or update commands.

In addition, it can print an optimized, PSR-0/4-compliant class index, which is also for performance considerations. There will be many class files in a large application, and autoloader will take up a large part of the time of each request. Using classmaps may not be convenient during development, but it can still get PSR-0/ 4 Convenience brought by norms.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us