Easy Tutorial
❮ Html5 Canvas Eccharts Android Tutorial Spinner ❯

Composer Installation and Usage

Category Programming Technology

Composer is a dependency management tool for PHP. You can declare the external libraries your project depends on, and Composer will install these dependencies for you. With it, we can easily use a command to incorporate other people's excellent code into our project.

By default, Composer is not installed globally but within a specified directory of the project (e.g., vendor).

Composer requires PHP 5.3.2+ and must have openssl enabled.

Composer can run on Windows, Linux, and OSX platforms.


Composer Installation

Windows Platform

On Windows, simply download Composer-Setup.exe and follow the installation steps.

Note that you need to enable openssl configuration. Open the php.ini file in the PHP directory and remove the semicolon before extension=php_openssl.dll.

After successful installation, you can check if it was installed correctly by entering composer --version in the command window (cmd).

Next, you can change to the Aliyun Composer full mirror:

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

To unset the configuration:

composer config -g --unset repos.packagist

Project Configuration

Modify the current project configuration only, so only the current project can use this mirror address:

composer config repo.packagist composer https://mirrors.aliyun.com/composer/

To unset the configuration:

composer config --unset repos.packagist

Debugging

Adding -vvv to the composer command can output detailed information, like so:

composer -vvv require alibabacloud/sdk

Encountering Issues?

  1. It is recommended to first upgrade Composer to the latest version:

    composer self-update
    
  2. Execute the diagnostic command:

    composer diagnose
    
  3. Clear the cache:

    composer clear
    
  4. If the project has been installed via other sources, you need to update the composer.lock file:

    composer update --lock
    
  5. Try again

Linux Platform

On Linux, you can use the following commands to install:

# php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
# php composer-setup.php

All settings correct for using Composer
Downloading...

Composer (version 1.6.5) successfully installed to: /root/composer.phar
Use it: php composer.phar

Move composer.phar so that composer can be called globally:

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

Switch to the domestic mirror:

# composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

Update composer:

# composer selfupdate

Mac OS System

On Mac OS, you can use the following commands to install:

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
$ composer --version
Composer version 1.7.2 2018-08-16 16:57:12

Switch to the domestic mirror:

$ composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

Update composer:

$ composer selfupdate

Composer Usage

To use Composer, we need to create a composer.json file in the project directory, which describes the project's dependencies.

The file format is as follows:

{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}

The above file specifies that we need any version starting from 1.2 of monolog.

Next, simply run the following command to install the dependency package:

composer install

require Command

In addition to using the install command, we can also use the require command to quickly install a dependency without manually adding dependency information in composer.json:

$ composer require monolog/monolog

Composer will first find a suitable version, then update the composer.json file by adding the relevant information for the monolog/monolog package, download and install the related dependencies, and finally update the composer.lock file and generate the PHP autoload files.

update Command

The update command is used to update all packages in the project, or specific packages:

# Update all dependencies
$ composer update

# Update a specified package
$ composer update monolog/monolog

# Update specified multiple packages
$ composer update monolog/monolog symfony/dependency-injection

# Update packages using wildcard matching
$ composer update monolog/monolog symfony/*

Note that the version a package can be upgraded to is constrained by the version constraints, and the package will not be upgraded beyond the scope of the constraints. For example, if the version constraint in composer.json is ^1.10 and the latest version is 2.0, the update command cannot upgrade the package to version 2.0, only to the highest 1.x version. See the version constraints section for more details.

remove Command

The remove command is used to remove a package and its dependencies (if the dependencies are not used by other packages). If the dependencies are used by other packages, they cannot be removed:

$ composer remove monolog/monolog
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 0 updates, 2 removals
  - Removing psr/log (1.0.2)
  - Removing monolog/monolog (1.23.0)
Generating autoload files

search Command

The search command can be used to search for packages:

$ composer search monolog

This command will output the package and its description information. If you only want to output the package names, you can use the --only-name parameter:

$ composer search --only-name monolog

show Command

The show command can list information about the packages used in the current project:

# List all installed packages
$ composer show

# Filter using wildcards
$ composer show monolog/*

# Show information about a specific package
$ composer show monolog/monolog

Basic Constraints

Exact Version

You can tell Composer to install a specific version, for example: 1.0.2, specifying version 1.0.2.

Range

You can specify a range of packages using comparison operators. These operators include: >, >=, <, <=, !=.

You can define multiple ranges, using spaces or commas , for logical AND, and double pipes || for logical OR. AND has a higher precedence than OR. Examples:

You can also specify a version range using a hyphen -.

The left side of the hyphen indicates the >= version, and if the right side is not a complete version number, it will be completed with wildcards. For example, 1.0 - 2.0 is equivalent to >=1.0.0 &lt;2.1 (2.0 is equivalent to 2.0.*), and 1.0.0 - 2.1.0 is equivalent to >=1.0.0 <=2.1.0.

Wildcard

You can use wildcards to set versions. 1.0.* is equivalent to >=1.0 &lt;1.1.

Tilde ~

The tilde ~ operator is used to specify the minimum version and allows the last digit of the version number to be upgraded. For example, ~1.2 is equivalent to >=1.2 &lt;2.0.0, and ~1.2.3 is equivalent to >=1.2.3 &lt;1.3.0. For projects using Semantic Versioning as their version standard, this version constraint is very useful. For example, ~1.2 defines the minimum minor version, and you can upgrade to any version below 2.0 without compatibility issues, as minor version upgrades should not have compatibility issues according to Semantic Versioning. In simple terms, ~ defines the minimum version and allows the last digit of the version number to be upgraded (if you don't understand, please read the example again).

Note that if ~ is applied to the major version number, such as ~1, it will be treated as ~1.0, allowing only minor version upgrades, not major version upgrades.

Caret ^

The caret ^ operator is closely related to Semantic Versioning and allows upgrades to safe versions. For example, ^1.2.3 is equivalent to >=1.2.3 &lt;2.0.0, as versions before 2.0 should not have compatibility issues. For versions before 1.0, this constraint also considers safety issues, such as ^0.3 will be treated as >=0.3.0 &lt;0.4.0.


Version Stability

If you do not explicitly specify the stability of the version, Composer will default to -dev or -stable internally based on the operator used. For example:

Constraint Internal Constraint
1.2.3 =1.2.3.0-stable
>1.2 >1.2.0.0-stable
>=1.2 >=1.2.0.0-dev
>=1.2-stable >=1.2.0.0-stable
<1.3 <1.3.0.0-dev
<=1.3 <=1.3.0.0-stable
1 - 2 >=1.0.0.0-dev <3.0.0.0-dev
~1.3 >=1.3.0.0-dev <2.0.0.0-dev
1.4.* >=1.4.0.0-dev <1.5.0.0-dev

The minimum-stability configuration item defines the default behavior for package version selection regarding stability. The default is stable. Its values are as follows (ordered by stability): dev, alpha, beta, RC, and stable. In addition to modifying this configuration to change the default behavior, we can also install versions with different stability than the default configuration by using stability flags (such as @stable and @dev). For example:

{
    "require": {
        "monolog/monolog": "1.0.*@beta",
        "acme/foo": "@dev"
    }
}

** Click to Share Notes

Cancel

-

-

-

❮ Html5 Canvas Eccharts Android Tutorial Spinner ❯