Practical PHP Dependency Management tool composer Getting Started tutorial

Source: Internet
Author: User

PHP Dependency management Tools composer Getting Started tutorial

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).

Reference: http://www.lai18.com/content/319715.html
Http://docs.phpcomposer.com/00-intro.md 
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!



Extended Reading

Advanced knowledge of PHP series of technical Articles to organize the collection

1session mechanism and related application of Session

2php data collection from the Central Meteorological Observatory coverage of the National Weather code example

3smarty Custom Function Htmlcheckboxes Usage instance

4PHP session may cause concurrency problems

5PHP callback function How to use and precautions

6 Friends net about QQ related PHP code (study QQ excellent information)

Use Imagick to generate PSD file thumbnail tutorial in 7PHP

8PHP implements the method of saving browsing history page URLs to cookies

The method of using session in 9php to prevent users from illegally logging in the background

10 using Xdebug to analyze PHP programs to identify performance bottlenecks

11PHP Dependency management Tools composer Getting Started tutorial

12PHP developers should know 5 tips for Composer

13PHP implementation of the Get_headers function with timeout function

Get IP and geolocation class sharing in 14PHP

15php using Iconv to solve the problem of Chinese truncation

Support Path_info detailed under 16nginx

24 Cool PHP Libraries that 17PHP developers should know about (micro-framework)

How does 18HHVM improve PHP performance?

19php ways to turn on multiple processes

20PHP Exception Handling

21PHP Daemon Process Instance

Analysis of pipeline communication example of 22PHP multithreading programming

23PHP multi-process processing parallel Processing Task instance

24PHP implementing multi-process parallel task handlers using QPM

Analysis of multi-thread instances in 25PHP multiple threads

26xhprof Installation && Use

27 Recommended 11 PHP frameworks for Developers

The difference between 28Cookie and session-a good article to summarize

29PHP Self-brought session hidden Trouble (session file exclusive lock causes blocking)

30php compression and decompression of string methods

31php method of implementing plug-in mechanism using reflection

Data structure doubly linked list of 32PHP SPL standard library (spldoublylinkedlist)

33PHP SPL standard library data structure fixed length array (splfixedarray)

34XSS Attack Knowledge Primer Finishing

35PHP SPL standard library data structure queue (Splqueue) and Priority queue (Splpriorityqueue)

Streams in 36PHP detailed introduction

37php file system permissions issues and workarounds when running in fastcgi mode

38PHP Performance Analysis tool Xhprof installation Tutorial

3,900 degrees engineer the implementation principle and performance analysis of PHP function (II.)

4,000 degrees engineer to say the implementation principle and performance analysis of PHP function (III.)

4,100 degrees engineer the implementation principle and performance analysis of PHP function (i.)

42 correct PHP matches regular expressions for UTF-8 Chinese

A summary of the series functions of sockets in 43php

44PHP Socket Programming Detailed

45 How to hide the PHP version number on a Linux system server

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Practical PHP Dependency Management tool composer Getting Started tutorial

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.