Table of Contents
介绍
依赖管理
声明依赖关系
系统要求
安装 - *nix
下载Composer可执行程序
局部安装
全局安装
安装 - Windows
使用安装程序
手动安装
使用Composer
自动加载
参考
Home php教程 php手册 PHP依赖管理工具Composer入门

PHP依赖管理工具Composer入门

Jun 06, 2016 pm 08:08 PM
com composer php introduce rely getting Started Management tools

介绍 Composer是PHP中的一个依赖管理工具. 它可以让你声明自己项目所依赖的库,然后它将会在项目中为你安装这些库。 依赖管理 Composer不是包管理器。是的,它实际上和"包"或者库打交道,但是它是以项目为单位进行管理,把它们安装到你项目中的一个目录(例

Composer

介绍

Composer是PHP中的一个依赖管理工具. 它可以让你声明自己项目所依赖的库,然后它将会在项目中为你安装这些库。

依赖管理

Composer不是包管理器。是的,它实际上和"包"或者库打交道,但是它是以项目为单位进行管理,把它们安装到你项目中的一个目录(例如vendor)。默认情况下它不会以全局的方式安装任何东西。因此,它是一个依赖管理器。

这个想法并不新鲜,Composer的灵感是来自于node的npm和ruby的bundler。但是目前PHP还没有一个这样的工具。

Composer解决的问题是:

a) 你有一个依赖N多库的项目。

b) 这些库中一些又依赖于其他的库。

c) 你声明你所依赖的库。

d) Composer找出哪些包的哪个版本将会被安装,然后安装它们(也就是把它们下载到你的项目中)。

声明依赖关系

假设你正在创建一个项目,然后你需要一个日志操作的库。你决定使用monolog。为了把它加入到你的项目中,你需要做的就是创建一个名为composer.json的文件,其描述这个项目的依赖关系。

<code>{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}
</code>
Copy after login

我们简单的描述说我们的项目依赖某个monolog/monolog包,版本只要是以1.2开头的就行。

系统要求

Composer需要PHP 5.3.2+才能运行。一些灵敏的PHP设置和编译选项也是必须的,不过安装程序(installer)会警告你任何不兼容的地方。

如果想要从源码而不是简单的从zip压缩包中安装软件包的话,你将需要git,svn或者hg,这依赖于软件包是通过什么进行版本控制的。

Composer是兼容多平台的,并且我们力争使其在Windows,Linux和OSX上的运行无差异。

安装 - *nix

下载Composer可执行程序

局部安装

为了获取Composer,我们需要做两件事。第一个是安装Composer(前面说过了,这意味下载它到你的项目中):

<code>$ curl -sS https://getcomposer.org/installer | php
</code>
Copy after login

这只会检查一些PHP设置,然后下载composer.phar到你的工作目录中。这个文件是Composer二进制文件。它是一个PHAR (PHP archive),PHP的归档格式,也可以像其他命令一样在命令行上运行。

你可以使用--install-dir选项,并且提供一个目标目录(可以是绝对或者相对路径)从而把Composer安装到一个指定的目录:

<code>$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
</code>
Copy after login

全局安装

你可以把这个文件放到任何你想放的地方。如果你把它放到你的PATH中,你就可以全局访问它了。在类unix系统中你甚至可以使它可执行,并且调用的时候不需要php

你可以执行这些命令从而能够在你的系统上简单的访问composer

<code>$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
</code>
Copy after login

然后,只需要执行composer命令来运行Composer,而不是php composer.phar

安装 - Windows

使用安装程序

这是在你的机器上安装Composer最简单的方法。

下载并运行Composer-Setup.exe,它将会安装最新的Composer版本并且设置好PATH,然后你就可以在命令中的任何目录下调用composer了。

手动安装

切换到一个存在于PATH环境变量中的目录,然后执行安装代码片段来下载composer.phar:

<code>C:\Users\username>cd C:\bin
C:\bin>php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
</code>
Copy after login

创建一个新的以.bat结尾的composer文件:

<code>C:\bin>echo @php "%~dp0composer.phar" %*>composer.bat
</code>
Copy after login

关闭你当前的终端。打开一个新的终端测试一下:

<code>C:\Users\username>composer -V
Composer version 27d8904
C:\Users\username>
</code>
Copy after login

使用Composer

我们接下来要使用Composer来安装项目的依赖。如果你在当前目录下没有一个叫作composer.json的文件,请跳到基本使用章节。

为了解决并下载依赖,运行install命令:

<code>$ php composer.phar install
</code>
Copy after login

如果你是全局安装,并且目录下没有phar文件,那么运行这个:

<code>$ composer install
</code>
Copy after login

如果是上面的例子,这个操作将会下载monolog到vendor/monolog/monolog目录。

自动加载

除了下载库之外,Composer也会创建一个自动加载文件,这个文件能够自动加载Composer下载的库中所有的类。如果想使用它,只需要在你代码启动的地方加上如下代码:

<code>require 'vendor/autoload.php';
</code>
Copy after login

哇哦!现在开始使用monolog吧! 如果想进一步学习Composer,继续阅读「基本使用」章节。 如果想要找需要的package,到Packagist。

参考

  • 英文原文:http://getcomposer.org/doc/00-intro.md
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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundle Apr 18, 2025 am 11:48 AM

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

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.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to view the version number of laravel? How to view the version number of laravel How to view the version number of laravel? How to view the version number of laravel Apr 18, 2025 pm 01:00 PM

The Laravel framework has built-in methods to easily view its version number to meet the different needs of developers. This article will explore these methods, including using the Composer command line tool, accessing .env files, or obtaining version information through PHP code. These methods are essential for maintaining and managing versioning of Laravel applications.

Using Dicr/Yii2-Google to integrate Google API in YII2 Using Dicr/Yii2-Google to integrate Google API in YII2 Apr 18, 2025 am 11:54 AM

VprocesserazrabotkiveB-enclosed, Мнепришлостольностьсясзадачейтерациигооглапидляпапакробоглесхетсigootrive. LEAVALLYSUMBALLANCEFRIABLANCEFAUMDOPTOMATIFICATION, ČtookazaLovnetakProsto, Kakaožidal.Posenesko

How to simplify email marketing with Composer: DUWA.io's application practices How to simplify email marketing with Composer: DUWA.io's application practices Apr 18, 2025 am 11:27 AM

I'm having a tricky problem when doing a mail marketing campaign: how to efficiently create and send mail in HTML format. The traditional approach is to write code manually and send emails using an SMTP server, but this is not only time consuming, but also error-prone. After trying multiple solutions, I discovered DUWA.io, a simple and easy-to-use RESTAPI that helps me create and send HTML mail quickly. To further simplify the development process, I decided to use Composer to install and manage DUWA.io's PHP library - captaindoe/duwa.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

See all articles