Home Development Tools composer How to run composer

How to run composer

Aug 12, 2019 am 09:16 AM
composer

How to run composer

composer怎么运行?

1.简介

Composer 是 PHP 的一个依赖管理工具。它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们。

2.系统要求

运行 Composer 需要 PHP 5.3.2+ 以上版本。

Composer 是多平台的,它可以同时在 Windows 、 Linux 以及 OSX 平台上运行。

3.安装(ubuntu)

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

注:如果没有安装curl,可以通过以下命令安装

apt-get update
apt-get install curl
Copy after login

当你的 Composer 安装完毕之后,你可以实用下列命令查看是否安装成功

composer -v
Copy after login

注 如果上述方法由于某些原因失败了,你还可以通过 php 下载安装器:

php -r "readfile('https://getcomposer.org/installer');" | php
Copy after login

这将检查一些 PHP 的设置,然后下载 composer.phar 到你的工作目录中。这是 Composer 的二进制文件。这是一个 PHAR 包(PHP 的归档),这是 PHP 的归档格式可以帮助用户在命令行中执行一些操作。

你可以通过 --install-dir 选项指定 Composer 的安装目录(它可以是一个绝对或相对路径)

4.使用

要开始在你的项目中使用 Composer,你只需要一个 composer.json 文件。该文件包含了项目的依赖和其它的一些元数据。

首先创建一个 composer.json 文件,写入相应的包名和版本号,如

{    
    "require": {
        "monolog/monolog": "1.13.*"
    }
}
Copy after login

这是后就写入了一个依赖包,之后安装依赖包。获取定义的依赖到你的本地项目,之后在你的项目目录中(即 composer.json 所在目录)使用 Composer 运行 install 命令。

composer install
Copy after login

当然,如果是在 Windows 系统中,也可以通过调用 composer.phar 包来进行依赖包的安装。

php composer.phar install
Copy after login

执行 composer install,就进入自动安装,安装完成后会生成一个 composer.lock 文件,里面是特定的版本号名,需要这个文件和 composer.json 一起提交到版本管理里去。

最后,在需要更新依赖包的时候,可以使用以下命令

composer update
Copy after login

如果只想更新部分依赖

composer update monolog/monolog
Copy after login

5.自动加载

对于库的自动加载信息,Composer 生成了一个 vendor/autoload.php 文件。你可以在你项目的入口文件中引入它

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;
?>
Copy after login

这使得你可以很容易的使用第三方代码。例如:如果你的项目依赖 monolog,你就可以像这样开始使用这个类库,并且他们将被自动加载。

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;
$log = new Monolog\Logger(&#39;name&#39;);
$log->pushHandler(new Monolog\Handler\StreamHandler(&#39;app.log&#39;, Monolog\Logger::WARNING));
$log->addWarning(&#39;Foo&#39;);
?>
Copy after login

6.Packagist / Composer 中国全量镜像

由于墙的问题,所以会导致 Composer 的国外镜像经常无法正常的 install,所以推荐使用国内的镜像,使用方式如下

有两种方式启用本镜像服务:

系统全局配置: 即将配置信息添加到 Composer 的全局配置文件 config.json 中。详见”方法一“

将配置信息添加到某个项目的 composer.json 文件中。详见”方法二“

方法一:

修改 composer 的全局配置文件

打开命令行窗口(windows用户)或控制台(Linux、Mac 用户)并执行如下命令:

composer config -g repo.packagist composer https://packagist.phpcomposer.com
Copy after login

方法二:

修改当前项目的 composer.json 配置文件:

打开命令行窗口(windows用户)或控制台(Linux、Mac 用户),进入你的项目的根目录(也就是 composer.json 文件所在目录),执行如下命令:

composer config repo.packagist composer https://packagist.phpcomposer.com
Copy after login

上述命令将会在当前项目中的 composer.json 文件的末尾自动添加镜像的配置信息(你也可以自己手工添加):

"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}
Copy after login

7.使用 Composer 中的 autoload 实现自动加载命名空间

Composer 除了可以帮你安装所需要的依赖包以外,还可以实现自动加载命名空间的功能,当我们自己编写的函数库与类库需要自动加载时,我们就可以通过 composer.json 来实现。它类似于 php 中的 spl_autoload_register(), 其实如果你去查看 Composer 中的源代码,你会看到它的自动加载功能也是用了 spl_autoload_register() 这个函数。《具体可看此文章详细介绍》

我们在 composer.json 里添加如下代码:

{
    "autoload": {
        "psr-4": {
            "Test\\": "test/",
            "Testtwo\\": "testtwo/"
        }
    }
}
Copy after login

这个配置文件中有一个 autoload 段,其中有个 《PSR-4》,psr-4 是一个基于 psr-4 规则的类库自动加载对应关系,只要在其后的对象中,以 ”命名空间“: “路径” 的方式写入自己的类库信息修改完成后,之后,在执行下列命令,即可完成自动加载。

composer dumpautoload
Copy after login

注: "psr-4": {"Test\\": "test/"} 中的 "test/" 路径为相对于 composer.json 的路径

这个时候,你就可以调用你自己编写的函数库或者类库了

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;
$testClass = new \Test\Testclass();
?>
Copy after login

更多composer使用教程,请访问composer使用教程栏目!

The above is the detailed content of How to run composer. For more information, please follow other related articles on the PHP Chinese website!

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)

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.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Improve Doctrine entity serialization efficiency: application of sidus/doctrine-serializer-bundle Improve Doctrine entity serialization efficiency: application of sidus/doctrine-serializer-bundle Apr 18, 2025 am 11:42 AM

I had a tough problem when working on a project with a large number of Doctrine entities: Every time the entity is serialized and deserialized, the performance becomes very inefficient, resulting in a significant increase in system response time. I've tried multiple optimization methods, but it doesn't work well. Fortunately, by using sidus/doctrine-serializer-bundle, I successfully solved this problem, significantly improving the performance of the project.

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 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 quickly build Fecmall advanced project templates using Composer How to quickly build Fecmall advanced project templates using Composer Apr 18, 2025 am 11:45 AM

When developing an e-commerce platform, it is crucial to choose the right framework and tools. Recently, when I was trying to build a feature-rich e-commerce website, I encountered a difficult problem: how to quickly build a scalable and fully functional e-commerce platform. I tried multiple solutions and ended up choosing Fecmall's advanced project template (fecmall/fbbcbase-app-advanced). By using Composer, this process becomes very simple and efficient. Composer can be learned through the following address: Learning address

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:

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.

See all articles