search
HomeDevelopment ToolscomposerComposer dependency management (PHP tool)

Composer dependency management (PHP tool)

Stop searching everywhere for PHP extension packages. For modern languages, package managers are basically standard. Java has Maven, Python has pip, Ruby has gem, and Nodejs has npm. PHP uses PEAR, but PEAR has many pitfalls:

● Dependency processing is prone to problems

● The configuration is very complicated

● Difficult to use command line interface

Fortunately, we have Composer, a powerful tool for PHP dependency management. It's open source and simple to use, and it's easy to submit your own packages.

For example, if we don’t use a framework at the beginning and want a verification code, we must first go to Gihutb or other places to find a verification code class, then include it in the project, and then edit it As soon as it starts running, problems may arise in later project maintenance. If it is open source on Github, you can click watch to see if there are bug fixes or new versions released, and you can follow up on the upgrades in a timely manner.

If you download too many expansion packs, you will need various includes. There may also be namespace conflicts. You will need to change the namespace according to the project. If the expansion pack is upgraded, you will need to download it again. Edit, this is very inconvenient. So the Composer dependency management library was born.

The above are the more important demand scenarios of Compser.

1. Can easily install and upgrade expansion packs

2. Just include, no need to write include everywhere

3. Avoid namespace conflicts

I usually go to Github, Code Cloud and other platforms to find expansion packages, but now there is a website that integrates all packages. In other words, the current development method: first search on packagist, and then use Composer to install and upgrade.

Install Composer

For fool-proof installation, just click https://getcomposer.org/Composer-Setup.exe, download and install, and the installation program will be You download Composer and set your PATH environment variable so that you can simply call Composer from any directory.

During the installation process, you need to pay attention to finding the root directory of php.exe and selecting the correct PHP path. I won’t provide screenshots here because I haven’t downloaded it yet and I installed it manually.

The selected directory should look like this:

D:\phpStudy\php\php-7.0.12-nts\php.exe

The following will focus on manual installation. I think this method is very easy to use:

First Download a composer.phar file and place the phar file in the developer folder. You can do this as you wish, and there is no limit to which folder it should be placed in.

Then open the DOS window, or use the shortcut key windows R to enter cmd, use the following command, first enter the directory where you placed the phar file

D:\developer\composer>echo @php "%~dp0composer.phar" %*>composer.bat

The sign of successful installation is to enter on the command line

composer -v

Display the following content

Composer dependency management (PHP tool)

##When I see this, I assume that Composer has been installed successfully. In the Chinese LAN, use Composer It is relatively slow, but fortunately there is a domestic mirror. Execute the following command to switch to the domestic mirror. What the domestic mirror does is to cache all installation packages and metadata to the domestic computer room and accelerate it through the domestic CDN, so that there is no need to Make a request to a foreign website.

composer config -g repo.packagist composer https://packagist.phpcomposer.com

Doing this is equivalent to changing the configuration globally. I chose to modify the composer.json configuration file of the current project:

{
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
}

Composer Common Commands<strong></strong>

selfupdate<strong></strong>

Update composer itself, please execute composer selfupdate frequently to keep Composer always the latest version.

composer selfupdate

Equivalent to

composer self-update

dumpautoload<strong></strong>##When we change the autoload in the composer.json file, we need Execute composer dumpautoload to make autoload take effect immediately. Without having to execute install or update commands.

composer dumpautoload

Equivalent to

composer dump-autoload

dumpautoload command has two commonly used options:

--optimize (-o): Convert PSR-0/4 autoloading to classmap, for faster loading speeds. This is particularly suitable for production environments, but may take some time to run, so it is not currently the default.

--no-dev: Disable autoload-dev rules.

install

composer install

According to the composer.lock (lock file) or composer.json file in the current directory, Define dependencies and install dependency packages.

The install command will first check whether the composer.lock lock file exists. If it exists, it will download the version specified in the composer.lock file, ignoring the definition in the composer.json file.

# 查看 composer install 的帮助信息
composer install -h
# 只安装 require 中定义的依赖,而不安装 require-dev 中定义的依赖
composer install --no-dev

update<strong></strong>If you want to update your dependency version, or you modify the dependency relationship in composer.json, you want composer To perform update operations as defined in the composer.json file, use the update command.

composer update

require<strong></strong>

require 命令一般用来安装新的依赖包,并将依赖写入当前目录的 composer.json 文件中。

如果 composer.json 文件中,添加或改变了依赖,修改后的依赖关系将被安装或者更新。

composer require

 

你也可以直接在命令中指明需要安装的依赖包。

composer require barryvdh/laravel-ide-helper

 

--dev 选项和 require-dev 相对应。如果你的依赖包仅仅用于开发环境,建议加上 --dev 选项。

composer require --dev barryvdh/laravel-ide-helper

 

 

<strong>create-project</strong>

你可以使用 create-project 从现有的包中创建一个新的项目。

它相当于执行了 git clone 命令后,将这个包的依赖安装到它自己的 vendor 目录。

此命令有几个常见的用途:

你可以快速的部署你的应用。

你可以检出任何资源包,并开发它的补丁。

多人开发项目,可以用它来加快应用的初始化。

# 安装 Laravel 项目
composer create-project --prefer-dist laravel/laravel blog 5.5.*

 

如果没有指定版本号,就默认安装最新的版本。

--prefer-dist: 当有可用的包时,从 dist 安装。

phpStudy集成环境下 安装composer失败

报错提示:

The "https://getcomposer.org/versions" file could not be downloaded: failed to open stream: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
The "https://getcomposer.org/download/1.2.0/composer.phar.sig" file could not be downloaded: SSL: crypto enabling timeout
Failed to enable crypto
failed to open stream: operation failed

 

1、安装composer需要开启openssl拓展 而phpstudy默认是关闭的

2、将php目录下的ssleay32.dll,libeay32.dll以及php/ext文件夹下的:php_openssl.dll 3个文件拷贝到WINDOWS\system32 文件夹下。

3、openssl需要CA证书 phpstudy也是没有的

CA证书下载地址:

http://curl.haxx.se/docs/caextract.html

选中之后单击右键选择另存为

下载成功之后放到tmp文件夹下面

4、然后修改php.ini文件

openssl.cafile = "D:\phpStudy\tmp\cacert.pem"

 

5、重启phpStudy就可以了报错提示:

failed to open stream: HTTP request failed!

 

1、检查一下php的curl拓展是否开启

2、检查这两个配置是否开启。

allow_url_fopen = On
user_agent="PHP"

 

也可以这样配置 user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)")

模拟浏览器访问也是一个不错的选择

3、开启之后重启重启phpStudy就可以了

 

PS: openssl.cafile 配置选项, 是 PHP 5.6.0. 以上的版本才支持的

更多composer相关技术文章,请访问composer栏目:https://www.php.cn/tool/composer/

The above is the detailed content of Composer dependency management (PHP tool). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:cnblogs. If there is any infringement, please contact admin@php.cn delete
Composer: The Package Manager for PHP DevelopersComposer: The Package Manager for PHP DevelopersMay 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

The Integration of AI into Composer: Exploring PotentialThe Integration of AI into Composer: Exploring PotentialMay 01, 2025 am 12:02 AM

AI can show its strengths in the field of music creation. 1) AI generates music through machine learning and deep learning, enhancing diversity and innovation. 2) AI composers can assist composers and provide inspiration and creativity. 3) In actual applications, performance needs to be optimized to solve the problems of coherence and innovation in the generation of music.

Composer's Purpose: Managing Project Dependencies in PHPComposer's Purpose: Managing Project Dependencies in PHPApr 30, 2025 am 12:01 AM

We need Composer because it can effectively manage dependencies of PHP projects and avoid the hassle of version conflicts and manual library management. Composer declares dependencies through composer.json and uses composer.lock to ensure the version consistency, simplifying the dependency management process and improving project stability and development efficiency.

Composer: Aiding PHP Development Through AIComposer: Aiding PHP Development Through AIApr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

Becoming a Composer: Training, Education, and ExperienceBecoming a Composer: Training, Education, and ExperienceApr 28, 2025 am 12:11 AM

To become a composer, you need to master music theory, harmonization, counterpoint, and be familiar with the tone and performance skills of the instrument. Composers express emotions and stories through music, and the creative process involves the construction and improvement of ideas to works.

Identifying a Composer: The Essential ElementsIdentifying a Composer: The Essential ElementsApr 27, 2025 am 12:27 AM

The key steps to identifying a composer include: 1) analyzing the composer's stylistic characteristics, such as Beethoven's drama and power; 2) understanding the composer's historical background and cultural influence, such as Bach's Baroque style; 3) comprehensively analyzing the melody, harmony, rhythm and structure of the work to avoid misjudgment caused by relying solely on a single element.

Composer: The Future of AI in PHP DevelopmentComposer: The Future of AI in PHP DevelopmentApr 26, 2025 am 12:10 AM

Composer'sfutureinPHPdevelopmentwithAIincludes:1)AI-enhanceddependencymanagementforsuggestinglibraries,2)AI-drivencodegenerationfortailoredboilerplate,and3)predictivemaintenanceforupdatesandpatches,butfaceschallengeslikedataprivacyandAIbias.

The Skills and Qualities of a Composer: An OverviewThe Skills and Qualities of a Composer: An OverviewApr 25, 2025 am 12:03 AM

Becoming a successful composer requires skills such as music theory, instrumental performance and sound design, as well as keen inspiration to capture and constant work modification. Composers use these skills and traits to transform emotions and thoughts into musical works, which resonates with their listeners.

See all articles

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

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),