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是啥Composer是啥Dec 25, 2023 pm 03:06 PM

Composer是PHP的依赖管理工具,它允许开发者将第三方库和框架与自己的项目进行集成。它的主要功能包括:1、依赖管理;2、版本控制;3、自动加载;4、扩展开发;5、集成其他工具。它简化了PHP项目的依赖管理过程,确保项目的稳定性和可维护性。通过使用Composer,开发者可以更加高效地管理自己的项目和集成第三方库和框架。

四大步教你在Debian11上安装使用Composer!四大步教你在Debian11上安装使用Composer!Nov 08, 2022 pm 04:32 PM

本文由composer​教程栏目给大家介绍关于在Debian11上是怎么一步步安装,以及使用Composer的 ,非常详细哦~希望对需要的朋友有所帮助!

composer 怎么修改php路径composer 怎么修改php路径Oct 24, 2022 am 11:33 AM

composer修改php路径的方法:1、搜索“composer.bat”并复制到项目文件夹;2、编辑“composer.bat”,将内容修改为“@ECHO OFF php "%~dp0composer.phar" %*”即可。

PHP使用Composer安装和管理依赖包PHP使用Composer安装和管理依赖包Jun 18, 2023 pm 03:30 PM

在PHP开发中,我们经常要处理各种依赖包,这些依赖包可能是其他开发者编写的PHP库文件,也可能是一些第三方工具和框架。为了方便管理这些依赖包,我们可以使用Composer来进行相关的安装和管理工作。Composer是一个开源的PHP依赖管理工具,它可以帮助我们自动化安装、更新和卸载PHP依赖包。通过Composer,我们可以轻松地管理我们应用中的不同依赖,同

使用Composer和PHP包管理器的最佳实践使用Composer和PHP包管理器的最佳实践May 23, 2023 am 08:29 AM

随着PHP的日益流行,PHP开发人员面临着许多挑战,其中包括代码管理、可重用性和依赖性管理。这些问题可以使用包管理器来解决,而Composer是PHP最受欢迎的包管理器之一。在本文中,我们将探讨使用Composer和PHP包管理器的最佳实践,从而提高您的PHP开发效率和代码质量。何为Composer?Composer是一款PHP包管理器,它可以轻松管理PHP

创建composer项目的步骤创建composer项目的步骤Feb 19, 2024 pm 07:13 PM

Composer是一个PHP的依赖管理工具,可以帮助开发者有效地管理项目中的依赖关系。通过Composer,我们可以轻松地引入第三方库、框架以及其他项目所需的各种资源。创建一个Composer项目非常简单,只需按照以下步骤进行操作:首先需要确保在本地已经安装了Composer。可以在终端中运行composer-v命令来确认是否已经安装成功。在项目的根目录中

如何在composer上安装和使用如何在composer上安装和使用Feb 19, 2024 pm 09:38 PM

composer是PHP的依赖管理工具,可以方便地安装、更新和管理项目所需的第三方库和依赖。本文将介绍composer的安装与使用,并提供详细的代码示例。一、安装Composer要使用composer,首先需要将其安装到本地开发环境中。以下演示了在Windows系统中安装composer的步骤:打开Composer的官方网站(https://getcompo

composer动画怎么保存composer动画怎么保存Apr 09, 2024 pm 02:02 PM

要保存 Composer 动画,可以使用 Lottie 文件格式,具体步骤为:导出为 JSON 文件;使用 Lottie 工具创建 Lottie 文件;从 Lottie 文件导出为多种格式,包括 JSON、GIF、MP4、SWF、HTML。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

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.