Table of Contents
问题说明
简单解释
流程
流程一:新项目流程
流程二:项目协作者安装现有项目
流程三:为项目添加新扩展包
关于 composer.lock 文件
关于扩展包的安装方法
Home Backend Development PHP Tutorial 如何正确使用 Composer 安装 Laravel 扩展包

如何正确使用 Composer 安装 Laravel 扩展包

Jun 20, 2016 pm 12:33 PM

问题说明

我们经常要往现有的项目中添加扩展包,有时候因为文档的错误引导,如下图来自 这个文档 的:

composer update 这个命令在我们现在的逻辑中,可能会对项目造成巨大伤害。

因为 composer update 的逻辑是按照 composer.json 指定的扩展包版本规则,把所有扩展包更新到最新版本,注意,是 所有扩展包 ,举个例子,你在项目一开始的时候使用了 monolog,当时的配置信息是

"monolog/monolog": "1.*",
Copy after login

安装的是 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.

流程

接下来介绍几个日常生产的流程,来方便加深大家的理解。

流程一:新项目流程

  1. 创建 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 框架, 提升开发效率的微信公众号.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles