Php dependency management tool composer Getting Started Tutorial

Source: Internet
Author: User
Tags composer install
Composer is a dependency management tool for PHP. It allows you to declare the code library on which the project depends, and it will install them for you in your project. Composer is a dependency management tool for PHP. It allows you to declare the code library on which the project depends, and it will install them for you in your project.

Dependency Management

Composer is not a package manager. Yes, it involves "packages" and "libraries", but it is managed based on each project and installed in a directory of your project (such as vendor. By default, it does not install anything globally. Therefore, this is just dependency management.

This idea is not new, and Composer is strongly inspired by node's npm and ruby's bundler. At that time, there was no similar tool in PHP.

Composer will solve the problem for you as follows:

You have a project dependent on several databases.

Some libraries depend on other libraries.

You declare what you are dependent on.

Composer will find out which packages need to be installed and install them (download them to your project ).

Declare dependency

For example, if you are creating a project, you need a library for logging. You decided to use monolog. To add it to your project, you need to create a composer. json file, which describes the dependencies of the project.

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

We only need to point out that our project requires some monolog/monolog packages, any version starting from 1.2.

System requirements

To run Composer, PHP 5.3.2 + or later is required. Some sensitive PHP settings and compilation flags are also required, but a warning is thrown for any incompatible installation program.

We will install the package directly from the package source, rather than simply downloading the zip file. you need git, svn, or hg, depending on the version management system used by the package you load.

Composer is multi-platform, and we strive to make it run equally well on Windows, Linux, and OSX platforms.

Linux/Unix installation

Local installation

To truly obtain Composer, we need to do two things. First install Composer (similarly, it means it will be downloaded to your project ):

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

Note: If the above method fails for some reason, you can also download the installer through php>:

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

This will check some PHP settings and then download composer. phar to your working directory. This is the binary file of Composer. This is a PHAR package (PHP archive). This is the PHP archive format that can help you execute some operations in the command line.

You can use the -- install-dir option to specify the Composer installation directory (which can be an absolute or relative path ):

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

Global Installation

You can place this file anywhere. If you put it in the PATH directory of the system, you can access it globally. In Unix-like systems, you can even use it without the php prefix.

You can execute these commands to allow composer to make global calls in your system:

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

Note: If the appeal command fails to be executed because of the permission, use sudo to run the mv command again. Now you only need to run the composer command to use Composer without entering php composer. phar.

Global Installation (on OSX via homebrew)

Composer is part of the homebrew-php project.

brew updatebrew tap josegonzalez/homebrew-phpbrew tap homebrew/versionsbrew install php55-intlbrew install josegonzalez/php/composer

Install in Windows

Use the installer

This is the easiest way to install Composer on your machine.

Download and run the Composer-Setup.exe, which will install the latest version of Composer and set the environment variables of the system, so you can directly use the composer command in any directory.

Manual installation

Set the system environment variable PATH and run the installation command to download the composer. phar file:

C:\Users\username>cd C:\binC:\bin>php -r "readfile('https://getcomposer.org/installer');" | php

Note: If you receive a readfile error message, use the http link or enable php_openssl.dll in php. ini. Create a file composer. bat in the composer. phar Directory of the same level:

C:\bin>echo @php "%~dp0composer.phar" %*>composer.bat

Close the current command line window and open a new command line window for testing:

C:\Users\username>composer -VComposer version 27d8904

Use Composer

Now we will use Composer to install project dependencies.

To resolve and download dependencies, run the install command:

php composer.phar install

If you have performed global installation without the phar file in the current directory, use the following command instead:

composer install

Continue with the above example. here we will download monolog to the vendor/monolog directory.

Automatic loading

In addition to library download, Composer also prepares an automatic file to load all class files in the library downloaded by Composer. To use it, you only need to add the following line of code to the pilot file of your project:

require 'vendor/autoload.php';

Now we can use monolog!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.