Getting started with PHP dependency management tool Composer

Source: Internet
Author: User
The introduction of Composer is a dependency management tool in PHP. It allows you to declare the libraries that your project depends on, and then it will install these libraries for you in the project. The dependency management Composer is not the package manager. Yes, it actually deals with packages or libraries, but it manages them in projects and installs them to a directory in your project (for example

The introduction of Composer is a dependency management tool in PHP. It allows you to declare the libraries that your project depends on, and then it will install these libraries for you in the project. The dependency management Composer is not the package manager. Yes, it actually deals with "packages" or libraries, but it manages them in projects and installs them to a directory in your project (for example

Introduction

Composer is a dependency management tool in PHP. It allows you to declare the libraries that your project depends on, and then it will install these libraries for you in the project.

Dependency Management

Composer is not the package manager. Yes, it actually deals with "packages" or libraries, but it manages them in projects and installs them to a directory in your project (for examplevendor). By default, it does not install anything globally. Therefore, it is a dependency manager.

This idea is not new. Composer is inspired by node npm and ruby bundler. However, PHP does not yet have such a tool.

Composer solves the following problems:

A) You have a project that depends on N multiple databases.

B) Some of these libraries depend on other libraries.

C) you declare the library on which you are dependent.

D) Composer find out which packages will be installed and then install them (that is, download them to your project ).

Declare dependency

Suppose you are creating a project, and then you need a logstore for log operations. You decided to use monolog. To add it to your project, you need to create a project namedcomposer.json.

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

We briefly describe that our project depends onmonolog/monologPackage, as long as the version is1.2.

System Requirements

Composer needs PHP 5.3.2 + to run. Some sensitive PHP settings and compilation options are also required, but the installer will warn you of any incompatibility.

If you want to install software packages from the source code rather than from the zip package, you will need git, svn, or hg, depending on the version control of the software package.

Composer is compatible with multiple platforms, and we strive to make it run smoothly on Windows, Linux, and OSX.

Install-* nix downloads the Composer executable program for local Installation

To obtain Composer, we need to do two things. The first one is to install Composer (as mentioned earlier, it means to download it to your project ):

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

This will only check some PHP settings and then downloadcomposer.pharTo your working directory. This file is a Composer binary file. It is a PHAR (PHParchive), PHP archive format, and can be run on the command line like other commands.

You can use--install-dirAnd provide a target directory (which can be an absolute or relative path) to install Composer to a specified directory:

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

You can put this file anywhere you want. If you put it in yourPATHYou can access it globally. In unix-like systems, you can even make it executable, and do not need to callphp.

You can execute these commands to make it easy to access your system.composer:

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

Then, you only need to executecomposerCommand to run Composer, insteadphp composer.phar.

Install-use the installer for Windows

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

Download and run composer-setup.exe. It will install the latest Composer version and set the PATH. Then you can callcomposer.

Manual Installation

Switch toPATHEnvironment Variable directory, and then execute the installation code snippet to download composer. phar:

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

Create a new.batComposer file at the end:

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

Close your current terminal. Open a new terminal and test it:

C:\Users\username>composer -VComposer version 27d8904C:\Users\username>
Use Composer

Next we will use Composer to install project dependencies. If you do not have anycomposer.jsonTo the basic usage section.

To solve and download dependencies, runinstallCommand:

$ php composer.phar install

If you install the SDK globally without a phar file in the directory, run the following command:

$ composer install

In the example above, this operation will download monologvendor/monolog/monologDirectory.

Automatic Loading

In addition to the download library, Composer also creates an automatically loaded file that can automatically load all classes in the library downloaded by Composer. If you want to use it, you only need to add the following code in the place where your code starts:

require 'vendor/autoload.php';

Wow! Use monolog now! To learn more about Composer, continue to the "basic usage" section. If you want to find the required package, go to Packagist.

Reference
  • Http://getcomposer.org/doc/00-intro.md.

Original article address: getting started with PHP dependency management tool Composer. Thank you for sharing it with me.

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.