问题说明
我们经常要往现有的项目中添加扩展包,有时候因为文档的错误引导,如下图来自 这个文档 的:
composer update 这个命令在我们现在的逻辑中,可能会对项目造成巨大伤害。
因为 composer update 的逻辑是按照 composer.json 指定的扩展包版本规则,把所有扩展包更新到最新版本,注意,是 所有扩展包 ,举个例子,你在项目一开始的时候使用了 monolog,当时的配置信息是
"monolog/monolog": "1.*",
安装的是 monolog 1.1 版本,而一个多月以后的现在,monolog 已经是 1.2 了,运行命令后直接更新到 1.2,这时项目并没有针对 1.2 进行过测试,项目一下子变得很不稳定,情况有时候会比这个更糟糕,尤其是在一个庞大的项目中,你没有对项目写完整覆盖测试的情况,什么东西坏掉了你都不知道。
那应该使用哪个命令呢?install, update 还是 require ?
接下来我们一一解释。
简单解释
- composer install - 如有 composer.lock 文件,直接安装,否则从 composer.json 安装最新扩展包和依赖;
- composer update - 从 composer.json 安装最新扩展包和依赖;
- composer update vendor/package - 从 composer.json 或者对应包的配置,并更新到最新;
- composer require new/package - 添加安装 new/package , 可以指定版本,如: composer require new/package ~2.5.
流程
接下来介绍几个日常生产的流程,来方便加深大家的理解。
流程一:新项目流程
- 创建 composer.json ,并添加依赖到的扩展包;
- 运行 composer install ,安装扩展包并生成 composer.lock ;
- 提交 composer.lock 到代码版本控制器中,如:git;
流程二:项目协作者安装现有项目
- 克隆项目后,根目录下直接运行 composer install 从 composer.lock 中安装 指定版本 的扩展包以及其依赖;
此流程适用于生产环境代码的部署。
流程三:为项目添加新扩展包
- 使用 composer require vendor/package 添加扩展包;
- 提交更新后的 composer.json 和 composer.lock 到代码版本控制器中,如:git;
关于 composer.lock 文件
composer.lock 文件里保存着对每一个代码依赖的版本记录(见下图),提交到版本控制器中,并配合 composer install 使用,保证了团队所有协作者开发环境、线上生产环境中运行的代码版本的一致性。
关于扩展包的安装方法
那么,准备添加一个扩展包,install, update, require 三个命令都可以用来安装扩展包,选择哪一个才是正确的呢?
答案是:使用 composer require 命令
另外,在手动修改 composer.json 添加扩展包后, composer update new/package 进行指定扩展包更新的方式,也可以正确的安装,不过不建议使用这种方法,因为,一旦你忘记敲定后面的扩展包名,就会进入万劫不复的状态,别给自己留坑呀。
上面的概念不论对新手或者老手来说,都比较混淆,主要记住这个概念:
原有项目新添加扩展的,都使用 composer require new/package 这种方式来安装。
完。
欢迎关注 LaravelTips , 这是一个专注于为 Laravel 开发者服务, 致力于帮助开发者更好的掌握 Laravel 框架, 提升开发效率的微信公众号.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.