search

Review composer

Dec 02, 2020 pm 03:07 PM
composerphp

The following tutorial column will take you through composer to review composer. I hope it will be helpful to friends in need!

Review composer

#Composer is a dependency management tool recommended by the PHP community. Composer is to PHP what npm is to Node. It is almost a necessary skill for modern PHP development. This article briefly reviews related concepts and usage of Composer.

Extensions and packages

The related concepts are frameworks and libraries. For the difference between frameworks and libraries, you can check out this article I wrote before

Extensions and packages They are two very similar concepts. In the PHP world, the two can generally be understood and distinguished in this way: extension and module are equivalent, and are a collection of functions written in C language; package and library are equivalent, and are mainly written in C language. A collection of functions implemented by PHP; extensions are loaded in the form of dynamic link libraries (dll or so), and packages are loaded through require/include. Most of the time, mixing the two will not cause difficulty in understanding.

Common extensions include GD, ZIP, XML, MySQLi, OPCache, etc. Common packages include PHPMailer, PHPOffice, HTMLPurifier, etc.

PEAR and PECL

Before Composer became popular, PEAR and PECL were the two tools (community) better known to PHP developers. PEAR is the abbreviation of PHP Extension and Application Repository, the official website is http://pear.php.net; PECL is the abbreviation of PHP Extension Community Library, the official website is http://pecl. php.net.

The difference between the two can be distinguished by extensions and packages: PECL hosting extensions, the source codes are mostly C files, such as APC, AMPQ, etc.; PEAR hosting packages, functions are implemented in PHP, such as PHP CodeSniffer, HTTP Request, etc. ;PEAR corresponds to the pear command, and PECL corresponds to the pecl command. You can use these two commands to install and manage extensions and packages (pear's build/pickle subcommand can also compile extensions in PECL). The two complement each other, and the official website describes their relationship as sisters.

PECL is a supplement to the official expansion and is still active. Some excellent expansions have the potential to become official expansions. Master Han Tianfeng’s swoole expansion is also hosted in PECL and is very well-known in China. In comparison, PEAR is a thing of the past. The emergence of PEAR2 and Pyrus (the next generation PEAR package installation tool, built based on PHP5.3, official website http://pear2.php.net) has not been able to save PEAR. The decline of PEAR is accompanied by the rise of Composer, the protagonist of this article.

PEAR's positioning is to "provide reusable PHP components" and provide developers with function packages in a centralized manner. The centralized release method ensures the quality of the code, but also brings inconvenience in maintenance: only packages that pass the review can be released, and the package obsolescence phenomenon is serious. The packages installed by PEAR are global, and dependent packages cannot be installed for individual projects. Unprivileged users cannot install dependent packages by themselves. Other disadvantages include poor dependency management. With the popularity of Github and the emergence of Composer, package management has entered the Composer era. PEAR has completed its historical mission and can go with peace of mind.

Composer

Strictly speaking, Composer is positioned as a dependency management tool rather than a package manager. Composer Chinese website introduces the work of Composer as follows:

Composer will solve the problem for you like this:

a) You have a project that depends on several libraries.

b) Some of these libraries depend on other libraries.

c) You declare what you depend on.

d) Composer will figure out which versions of packages need to be installed, and install them (download them into your project).

Composer can do everything PEAR can do (including installing PECL extensions), and some can do it better. Composer installs the package in the project directory by default, and ordinary users can use it normally (Composer officially recommends not to execute composer commands as root); it is encouraged to follow best practices (i.e. the famous PSR specification, for details, see the PHP-FIG official website https:/ /www.php-fig.org), which greatly promotes the standardization of coding style in the PHP community; Composer is a decentralized platform where anyone can publish code packages; there is no need to review the package when publishing it, and the quality of the package is determined by user voting. .As the successor of PEAR, Composer's performance has withstood the test of the community and has become the de facto standard tool for dependency management.

Composer has now formed a huge ecosystem, and in terms of quantity, Composer’s packages far exceed PEAR. Since anyone can publish packages freely without review, packages in the Composer ecosystem may have hidden concerns such as uneven code quality, different code styles, and backdoor vulnerabilities. In addition, Composer's dependency management is based on projects, and the same package may be installed multiple times on a machine. But its flaws outweigh its flaws. Overall, Composer has greatly changed the PHP development ecosystem and promoted code exchange and community development.

Composer usage

Composer is born to manage the dependencies of projects, and the composer.json file in the project is the basis for its work. The most important part of the file is the require section, which tells Composer which packages it expects to install and their versions, for example:

{
    "name": "tlanyan/foo",
    "version": "1.0.0",
    ....
    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": ">=2.0.6",
        "yiisoft/yii2-swiftmailer": "*",
        "yiisoft/yii2-redis": ">=2.0.0",
        "smarty/smarty": "< =3.1.25",
        "yiisoft/yii2-smarty": ">=2.0.0",
        "phpoffice/phpexcel": ">=1.8.0",
        "tecnickcom/tcpdf": "~6.2.0"
    },
    ....
}

然后运行composer install命令,Composer会自动分析依赖,安装最合适的包到vendor目录下。加-v(-vv, -vvv)选项会打印命令执行过程中的详细信息。安装完毕后,vendor目录下会生成autoload.php文件。在项目的入口文件中包含此文件: require __DIR__ . "/vendor/autoload.php";,接下来便可在项目的任何地方引用依赖包中的接口和类。

install命令,Composer提供了许多其他命令管理依赖。常用的命令场景包括:查找依赖、引入依赖、安装依赖、更新依赖。分别对应的命令是:

  1. composer search: 根据关键字查找依赖包,例如查找本人发布的包:composer search tlanyan。该命令等同于上https://packagist.org进行包查找;
  2. composer require: 引入依赖,声明项目或者全局(global,用户名全局,非系统全局)依赖某个包, 例如声明需要swiftmailer包: composer require [global] "swiftmailer/swiftmailer:dev-master";该命令更新composer.json文件,并默认立即安装依赖(--no-update选项可阻止默认安装);效果等同于编辑composer.json文件,然后执行install命令;
  3. composer install:安装composer.json声明的依赖包,最终安装的依赖包版本可能取决于有无composer.lock文件;
  4. composer update: 更新依赖到最新版本,相当于删除composer.lock文件后执行composer install

以上四条命令涵盖使用Composer的大部分场景。以下是几个常用的辅助命令,与依赖分析相关:

  1. composer info: 查看安装的依赖包信息,与composer show等价;
  2. composer dumpautoload: 加-o选项可导出优化的加载器;
  3. composer why(-not): 查看(不)安装某个包的原因。

总结

从拷贝第三方代码到项目中(1994),到PEAR安装依赖包(1999),再到Composer兴起(2012),PHP社区经历了将近20年的探索。PHP这门古老的语言,也在不断的发展更新,在web领域一直发光发热。Composer作为目前PHP包依赖管理的最佳工具,值得每一位PHP开发人员掌握。

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

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
Composer and AI: New Possibilities in PHP DevelopmentComposer and AI: New Possibilities in PHP DevelopmentApr 19, 2025 am 12:03 AM

The combination of AI and Composer can improve PHP development efficiency and security. Specifically reflected in: 1. Dependency analysis and optimization: AI can predict dependencies and reduce conflicts. 2. Automated security checks: AI can identify security vulnerabilities, and it is recommended to update them. 3. Code generation and optimization: AI can automatically generate and optimize related code.

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

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

How to use Composer to resolve JSON Schema verification issuesHow to use Composer to resolve JSON Schema verification issuesApr 18, 2025 am 11:51 AM

I'm having a tricky problem when developing a Symfony-based application: how to effectively validate JSON data format. Initially, I tried using manual verification code, but this was not only complicated, but also error-prone. After some exploration, I discovered a Composer package called ptyhard/json-schema-bundle, which brought great convenience and efficiency to my project.

Use Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundleUse Composer to solve the dilemma of recommendation systems: andres-montanez/recommendations-bundleApr 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 quickly build Fecmall advanced project templates using ComposerHow to quickly build Fecmall advanced project templates using ComposerApr 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

Improve Doctrine entity serialization efficiency: application of sidus/doctrine-serializer-bundleImprove Doctrine entity serialization efficiency: application of sidus/doctrine-serializer-bundleApr 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.

Use Composer to resolve error logging issues in Laravel projectsUse Composer to resolve error logging issues in Laravel projectsApr 18, 2025 am 11:39 AM

When developing Laravel projects, the management of error logs is a very critical link. Recently, I encountered a problem in my project: how to efficiently capture and record all types of errors and ensure that these error messages can be processed in a timely manner. After some research, I found the lukeboy25/errorlogger package. It is installed through Composer and can greatly simplify the management process of error logs. You can learn composer through the following address:

How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa libraryHow to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa libraryApr 18, 2025 am 11:36 AM

When developing a Laravel application, I encountered a common but difficult problem: how to improve the security of user accounts. With the increasing complexity of cyber attacks, a single password protection is no longer enough to ensure the security of users' data. I tried several methods, but the results were not satisfactory. Finally, I installed the wiebenieuwenhuis/laravel-2fa library through Composer and successfully added two-factor authentication (2FA) to my application, greatly improving security.

See all articles

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)