php依赖composer_使用Composer管理PHP依赖关系

php依赖composer

Warning: this post is old and might not reflect the current state of the art

警告:此帖子过时,可能无法反映当前的最新状态

Composer has proven to be the best modern dependency management solution for PHP.

实践证明, Composer是适用于PHP的最佳现代依赖管理解决方案。

Composer filled a need and now is on the same level as what npm is for Node.js packages.

Composer满足了需求,现在与Node.js软件包的npm处于同一水平。

Combined with Packagist, the official repository for Composer packages, it’s a utility you cannot avoid using.

Packagist (Composer软件包的官方存储库)结合使用,它是您无法避免使用的实用程序。

在OSX / Linux上安装Composer (Installing Composer on OSX / Linux)

curl -sS https://getcomposer.org/installer | php

curl -sS https://getcomposer.org/installer | php

and

mv composer.phar /usr/local/bin/composer

mv composer.phar /usr/local/bin/composer

More details on installation

有关安装的更多详细信息

使用作曲家 (Using Composer)

Initialize Composer by typing

通过键入初始化Composer

composer init

in the root of the project. Follow the instructions, you’ll end up with a composer.json file. This is the file that contains all the project dependencies.

在项目的根目录中。 按照说明进行操作,最后得到一个composer.json文件。 这是包含所有项目依赖项的文件。

Now type

现在输入

composer install

This command will download all the dependencies in the vendor folder and create a composer.lock file.

此命令将下载vendor文件夹中的所有依赖项,并创建一个composer.lock文件。

The vendor folder contains the packages folders, plus an autoload.php file and the composer folder, which are internal Composer files.

vendor文件夹包含程序包文件夹,以及内部Composer文件的autoload.php文件和composer文件夹。

To include the dependencies include vendor/autoload.php in your PHP main file:

要包括依赖关系,请在您PHP主文件中包含vendor/autoload.php

require 'vendor/autoload.php'

and all will just work.

一切都会工作。

添加依赖 (Adding dependencies)

To add more dependencies to the project type

向项目类型添加更多依赖项

composer require packagename:1.0

Example:

例:

composer require filp/whoops:2.0

更新依赖项 (Updating dependencies)

Run

composer update

Composer will search for updates for the packages you specified, taking into consideration any versions limit you added, and will update the composer.lock file.

考虑到您添加的任何版本限制,Composer将搜索您指定的软件包的更新,并将更新composer.lock文件。

套件版本 (Package versions)

Most used version formats:

最常用的版本格式:

  • “1.0”: will install that precise version

    “ 1.0”:将安装该精确版本
  • “~1.0”: will install any update to 1.0, 1.0.1, 1.0.20 but will not install version 1.1 and later versions.

    “〜1.0”:将安装对1.0、1.0.1、1.0.20的任何更新,但不会安装1.1版和更高版本。

作曲家 (composer.lock)

Whenever you update the installed dependencies running composer install or composer update, the composer.lock file will be updated to reflect it. So if you add this file to the repository everyone that downloads the project and runs composer install will get the exact same versions you installed, even if a newer one satisfying the version limits in composer.json is available.

每当您更新运行composer installcomposer update update的已安装依赖项时, composer.lock文件都会被更新以反映它。 因此,如果将此文件添加到存储库中,那么即使可以使用满足composer.json版本限制的较新版本,下载该项目并运行composer install每个人也将获得与您安装的版本完全相同的版本。

They need to run composer update to get the latest releases available.

他们需要运行composer update以获得可用的最新版本。

软件包更新通知 (Package updates notifications)

Add your project’s composer.json and composer.lock to VersionEye to be notified via email when your dependencies are updated, so you can update them in your project.

将您的项目的composer.jsoncomposer.lock添加到VersionEye ,以便在依赖项更新时通过电子邮件收到通知,因此您可以在项目中对其进行更新。

依赖项中的安全性问题 (Security issues in your dependencies)

Add your composer.lock to Security Advisories Checker

将您的composer.lock添加到安全公告检查器

关于梨的注意事项 (A note on PEAR)

PEAR was the older widely used package manager. The major disadvantage over Composer is that dependencies are installed globally, so there is no version fine-tuning allowed for projects. Once you update a PEAR package, every project on the system will use the new version, and depending on your scenario, this might be a problem.

PEAR是较早使用的软件包管理器。 与Composer相比,主要缺点是全局安装了依赖项,因此项目不支持任何版本微调。 一旦更新了PEAR软件包,系统上的每个项目都将使用新版本,并且根据您的情况,这可能是一个问题。

翻译自: https://flaviocopes.com/php-dependencies/

php依赖composer

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值