Home  >  Article  >  Development Tools  >  Composer dependency management (PHP tool)

Composer dependency management (PHP tool)

藏色散人
藏色散人forward
2020-01-26 16:01:354096browse

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.com. If there is any infringement, please contact admin@php.cn delete