Home Development Tools composer Common commands and version constraints of Composer

Common commands and version constraints of Composer

Aug 20, 2019 pm 02:36 PM
composer

The following column of composer usage tutorial will explain commonly used package management commands and how to constrain package versions. I hope it will be helpful to friends in need!

Common commands and version constraints of Composer

Common commands

require command

In "Composer Tutorial " has briefly introduced how to use the install command to install dependencies. In addition to 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
Using version ^1.19 for monolog/monolog
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing psr/log (1.0.0)
    Downloading: 100%         
 
  - Installing monolog/monolog (1.19.0)
    Downloading: 100%         
 
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
......
monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
Writing lock file
Generating autoload files
Copy after login

Composer will first find the appropriate version and then update the composer.json file. Add the relevant information of the monolog/monolog package in require, then download the relevant dependencies for installation, and finally update the composer.lock file and generate the PHP automatic loading file.

update command

Through the update command, you can update all packages in the project, or certain specified packages.

# 更新所有依赖
$ composer update
 
# 更新指定的包
$ composer update monolog/monolog
 
# 更新指定的多个包
$ composer update monolog/monolog symfony/dependency-injection
 
# 还可以通过通配符匹配包
$ composer update monolog/monolog symfony/*
Copy after login

It should be noted that the version that the package can upgrade will be restricted by the version constraint, and the package will not be upgraded to a version beyond the constrained range. For example, if the version constraint of the package in composer.json is ^1.10, and the latest version is 2.0. Then the update command cannot upgrade the package to version 2.0, but can only upgrade it to version 1.x. Please see the introduction below for version constraints.

remove command

Use the remove command to remove a package and its dependencies (when the dependencies are not used by other packages):

$ composer remove monolog/monolog
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing monolog/monolog (1.19.0)
  - Removing psr/log (1.0.0)
Writing lock file
Generating autoload files
Copy after login

search command

Use the search command to search for packages:

$ composer search monolog
monolog/monolog Sends your logs to files, sockets, inboxes, databases and various web services
 
# 如果只是想匹配名称可以使用--only-name选项
$ composer search --only-name monolog
Copy after login

show command

Use the show command to list Get information about the packages currently installed in the project:

# 列出所有已经安装的包
$ composer show
 
# 可以通过通配符进行筛选
$ composer show monolog/*
 
# 显示具体某个包的信息
$ composer show monolog/monolog
Copy after login

The above is an introduction to commonly used commands.

Version Constraints

As mentioned earlier, we can specify the version of the package to be downloaded. For example, we want to download version 1.19 of monolog. We can achieve the goal through the composer.json file:

{
    "require": {
        "monolog/monolog": "1.19"
    }
}
Copy after login

and then run the install command, or through the require command:

$ composer require monolog/monolog:1.19
 
# 或者
$ composer require monolog/monolog=1.19
 
# 或者
$composer require monolog/monolog 1.19
Copy after login

In addition to specifying the specific version as above, we can also pass different constraints way to specify the version.

Basic constraints

Precise version

You can specify a specific version to tell Composer that only this version can be installed. But if other dependencies require other versions, the package installation or update will eventually fail and terminate.

Example: 1.0.2

Scope

You can specify the scope of a package using comparison operators. These operators include: >, >=, <, <=, !=.

You can define multiple ranges, use spaces or commas to indicate logical AND, and use double vertical bars || to indicate logical OR. The priority of AND will be greater than or.

It should be noted that using an unbounded range may cause unpredictable versions to be installed and break downward compatibility. It is recommended to use the hyphen operator.

Example:

>=1.0
>=1.0 <2.0
>=1.0 <1.1 || >=1.2
Copy after login

Range (use hyphen)

The hyphenated range indicates the included version range, which means that there must be borderline. The left side of the hyphen indicates the >= version, while the situation on the right side of the hyphen is a little more complicated. If the version on the right is not a complete version number, it will be completed using wildcard characters. For example, 1.0 - 2.0 is equivalent to >=1.0.0 <2.1 (2.0 is equivalent to 2.0.*), while 1.0.0 - 2.1.0 is equivalent to >=1.0.0 <=2.1.0.

Example: 1.0 - 2.0

Wildcards

You can use wildcards to define versions. 1.0.* is equivalent to >=1.0 <1.1.

Example: 1.0.*

Next Major Version Operator

tilde~

## Let's first explain the usage of the ~ operator through the following example: ~1.2 is equivalent to >=1.2 <2.0.0, and ~1.2.3 is equivalent to >=1.2.3 <1.3.0. This version constraint method is very practical for projects that use Semantic Versioning as the version number standard. For example, ~1.2 defines the smallest minor version number, and then you can upgrade any version below 2.0 without problems, because according to the version definition of Semantic Versioning, minor version upgrades should not have compatibility issues. Simply put, ~ defines the minimum version and allows the last digit of the version number to be upgraded (if you don’t understand, please look at the previous example again).

Example: ~1.2

It should be noted that if ~ acts on the major version number, such as ~1, according to the above statement, Composer can install major versions after version 1, but In fact, ~1 will be treated as ~1.0, and only minor versions can be added, not major versions.

fold number^

^操作符的行为跟Semantic Versioning有比较大的关联,它允许升级版本到安全的版本。例如,^1.2.3相当于>=1.2.3 <2.0.0,因为在2.0版本前的版本应该都没有兼容性的问题。而对于1.0之前的版本,这种约束方式也考虑到了安全问题,例如^0.3会被当作>=0.3.0 <0.4.0对待。

例子:^1.2.3

版本稳定性

如果你没有显式的指定版本的稳定性,Composer会根据使用的操作符,默认在内部指定为-dev或者-stable。例如:

Common commands and version constraints of Composer

如果你想指定版本只要稳定版本,你可以在版本后面添加后缀-stable。

minimum-stability 配置项定义了包在选择版本时对稳定性的选择的默认行为。默认是stable。它的值如下(按照稳定性排序):dev,alpha,beta,RC和stable。除了修改这个配置去修改这个默认行为,我们还可以通过稳定性标识(例如@stable和@dev)来安装一个相比于默认配置不同稳定性的版本。例如:

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

以上是版本约束的介绍。

The above is the detailed content of Common commands and version constraints of Composer. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

How to simplify email marketing with Composer: DUWA.io's application practices How to simplify email marketing with Composer: DUWA.io's application practices Apr 18, 2025 am 11:27 AM

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to view the version number of laravel? How to view the version number of laravel How to view the version number of laravel? How to view the version number of laravel Apr 18, 2025 pm 01:00 PM

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa library How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa library Apr 18, 2025 am 11:36 AM

When developing a Laravel application, I encountered a common but difficult problem: how to improve the security of user accounts. With the increasing complexity of cyber attacks, a single password protection is no longer enough to ensure the security of users' data. I tried several methods, but the results were not satisfactory. Finally, I installed the wiebenieuwenhuis/laravel-2fa library through Composer and successfully added two-factor authentication (2FA) to my application, greatly improving security.

See all articles