Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Composer PHP relies on a new era of management


May 25, 2021 Composer


Table of contents


Composer PHP relies on a new era of management

For modern languages, package managers are basically standard. J ava has Maven, Python has pip, Ruby has gem, Nodejs has npm. PHP's is PEAR, but PEAR pits are many:

  • Dependency processing is prone to problems
  • The configuration is very complex
  • Difficult command-line interface

Fortunately, we have Composer, PHP-dependent management weapon. It's open source, it's easy to use, and it's easy to submit your own package.

Install Compaser

Composer requires PHP 5.3.2 plus to operate.

$ curl -sS https://getcomposer.org/installer | php

This command composer.phar to the current directory. PHAR (PHP package) is a compressed format that can run directly from the command line.

You can use --install-dir option to install Compasser to a specified directory, for example:

$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin

Of course, you can also do a global installation:

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

Homebrew can also be installed under Mac OS X:

brew tap josegonzalez/homebrew-php  
brew install josegonzalez/php/composer  

Typically, composer.phar PATH environment variable, not necessarily a global installation.

Declare a dependency

Create a composer.json file in the project directory that indicates dependencies, such as your project dependency monolog:

{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}

Installation dependency

Installation dependencies are simple and simply run in the project directory:

composer install  

If there is no global installation, run:

php composer.phar install  

Autoload

Composer provides the feature of autoloading by simply adding the following line to the initialization section of your code:

require 'vendor/autoload.php';  

The module warehouse

packagist.org is Comcoser's repository, where many well-known PHP libraries can be found. You can also submit your own work.

Advanced features

The basic usage of Compaser is described above. Composer also has some advanced features that, while not required, often facilitate PHP development.