PHP Dependency management Tools composer Getting Started tutorial

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

Dependency Management

Composer is not a package manager. Yes, it involves "packages" and "libraries", but it is managed on a per-project basis and installed in a directory of your project (for example, vendor). By default it will not install anything globally. Therefore, this is only a dependency management.

This idea is not new and Composer is strongly inspired by node's npm and Ruby S bundler. There was no such tool under PHP at the time.

Composer will solve this problem for you:

You have a project that relies on a number of libraries.

Some of these libraries depend on other libraries.

You declare what you depend on.

Composer will find out which version of the package needs to be installed and install them (download them to your project).

declaring dependencies

For example, you are creating a project and you need a library to do the logging. You decide to use Monolog. To add it to your project, all you need to do is create a Composer.json file that describes the dependencies of the project.

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

We just need to point out that our project needs some monolog/monolog packages, starting from 1.2 in any version.

System Requirements

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

Instead of simply downloading the zip file, we'll install it directly from the package's source, and you'll need git, svn, or HG, depending on the version management system you're loading the package with.

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

Linux/unix under Installation

Partial installation

To really get Composer, we need to do two things. First install Composer (again, which 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 via 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 a binary file of Composer. This is a PHAR package (PHP archive), which is a PHP archive format that helps users perform some operations on the command line.

You can specify the installation directory for Composer by using the--install-dir option (it can be an absolute or relative path):

Curl-ss Https://getcomposer.org/installer | PHP----install-dir=bin

Global Installation

You can put this file anywhere. If you put it in the PATH directory of the system, you will be able to access it globally. In Unix-like systems, you can even use PHP without a prefix.

You can execute these commands to let composer make a global call on your system:

Curl-ss Https://getcomposer.org/installer | PHPMV Composer.phar/usr/local/bin/composer

Note: If the appeal command fails because of permission execution, use sudo again to try running the MV command. Now just run the composer command to use composer without the need to enter 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 under Windows

Using the Setup program

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

Download and run Composer-setup.exe, which installs the latest version of Composer and sets up the environment variables for the system, so you can use the Composer command directly in any directory.

Manual Installation

Set the system's environment variable PATH and run the install 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, use the HTTP link or turn on Php_openssl.dll in php.ini. Create a new file Composer.bat in the Composer.phar sibling directory:

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

Close the current command-line window and open a new command-line window to test:

C:\users\username>composer-vcomposer version 27d8904

Using Composer

Now we will use Composer to install the dependencies of the project.

To resolve and download dependencies, execute the Install command:

PHP Composer.phar Install

If you have a global installation and do not have a Phar file in the current directory, use the following command instead:

Composer Install

Continuing with the above example, the Monolog to the Vendor/monolog/monolog directory will be downloaded here.

Auto Load

In addition to the library download, Composer also prepared an auto-load file that can load all the class files in the Composer downloaded library. To use it, you simply add the following line of code to your project's bootstrap file:

Require ' vendor/autoload.php ';

Now we can use the 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.