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 =1.0.0
Example: 1.0 - 2.0
Wildcards
You can use wildcards to define versions. 1.0.* is equivalent to >=1.0
Example: 1.0.*
Next Major Version Operator
tilde~
fold number^
^操作符的行为跟Semantic Versioning有比较大的关联,它允许升级版本到安全的版本。例如,^1.2.3相当于>=1.2.3 =0.3.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!

The combination of AI and Composer can improve PHP development efficiency and security. Specifically reflected in: 1. Dependency analysis and optimization: AI can predict dependencies and reduce conflicts. 2. Automated security checks: AI can identify security vulnerabilities, and it is recommended to update them. 3. Code generation and optimization: AI can automatically generate and optimize related code.

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

I'm having a tricky problem when developing a Symfony-based application: how to effectively validate JSON data format. Initially, I tried using manual verification code, but this was not only complicated, but also error-prone. After some exploration, I discovered a Composer package called ptyhard/json-schema-bundle, which brought great convenience and efficiency to my project.

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:

When developing an e-commerce platform, it is crucial to choose the right framework and tools. Recently, when I was trying to build a feature-rich e-commerce website, I encountered a difficult problem: how to quickly build a scalable and fully functional e-commerce platform. I tried multiple solutions and ended up choosing Fecmall's advanced project template (fecmall/fbbcbase-app-advanced). By using Composer, this process becomes very simple and efficient. Composer can be learned through the following address: Learning address

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 Laravel projects, the management of error logs is a very critical link. Recently, I encountered a problem in my project: how to efficiently capture and record all types of errors and ensure that these error messages can be processed in a timely manner. After some research, I found the lukeboy25/errorlogger package. It is installed through Composer and can greatly simplify the management process of error logs. You can learn composer through the following address:

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment