Common commands and version constraints of 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
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
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/*
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
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
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
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" } }
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
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
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~
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。例如:
如果你想指定版本只要稳定版本,你可以在版本后面添加后缀-stable。
minimum-stability 配置项定义了包在选择版本时对稳定性的选择的默认行为。默认是stable。它的值如下(按照稳定性排序):dev,alpha,beta,RC和stable。除了修改这个配置去修改这个默认行为,我们还可以通过稳定性标识(例如@stable和@dev)来安装一个相比于默认配置不同稳定性的版本。例如:
{ "require": { "monolog/monolog": "1.0.*@beta", "acme/foo": "@dev" } }
以上是版本约束的介绍。
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

I encountered a common but tricky problem when developing a large PHP project: how to effectively manage and inject dependencies. Initially, I tried using global variables and manual injection, but this not only increased the complexity of the code, it also easily led to errors. Finally, I successfully solved this problem by using the PSR-11 container interface and with the power of Composer.

I had a tough problem when working on a project with a large number of Doctrine entities: Every time the entity is serialized and deserialized, the performance becomes very inefficient, resulting in a significant increase in system response time. I've tried multiple optimization methods, but it doesn't work well. Fortunately, by using sidus/doctrine-serializer-bundle, I successfully solved this problem, significantly improving the performance of the project.

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.

I encountered a tricky problem when developing a new Laravel project: how to quickly build a fully functional and easy-to-manage content management system (CMS). I tried multiple solutions, but all gave up because of complex configuration and inconvenient maintenance. Until I discovered the LaravelCMS package mki-labs/espresso, which not only simple to install, but also provides powerful functions and intuitive management interface, which completely solved my problem.

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.

I'm having a tough problem when developing a complex web application: how to effectively handle JavaScript errors and log them. I tried several methods, but none of them could meet my needs until I discovered the library dvasilenko/alterego_tools. I easily solved this problem through the installation of this library through Composer and greatly improved the maintainability and stability of the project. Composer can be learned through the following address: Learning address

Summary Description: When dealing with complex data types, you often encounter problems of how to uniformly represent and operate. This problem can be easily solved with Composer using the phrity/o library. It provides encapsulation classes and traits for various data types, making data processing more consistent and efficient.
