Practical PHP dependency management tool Composer getting started tutorial, composer getting started tutorial

Source: Internet
Author: User
Tags spl php session composer install

Practical PHP dependency management tool Composer getting started tutorial, composer getting started tutorial

PHP dependency management tool Composer getting started tutorial

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 | php

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

php -r "readfile('');" | 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 | 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 | 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('');" | 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!

Additional reading

PHP advanced knowledge series technical articles 

1 session mechanism explanation and session-related applications

Example of weather forecast code collected by 2php from the Central Meteorological Station covering the whole country

3smarty User-Defined Function htmlcheckboxes usage instance

4 PHP Session may cause concurrency Problems

5PHP callback function usage and precautions

6 friend gateway's PHP Code related to QQ (study QQ's excellent materials)

How to Use imagick to generate PSD file thumbnails in 7PHP

How to save browsing history web pages to cookies in 8PHP

9php uses session to prevent unauthorized user logon to the background

10 use XDebug to analyze the PHP program and identify performance bottlenecks

11PHP dependency management tool Composer getting started tutorial

12 PHP developers should know five tips for Composer

The get_headers function with timeout implemented by 13PHP

Share IP addresses and geographic locations in 14PHP

Solution to iconv truncation problem in 15php

Support for PATH_INFO in 16nginx

17 PHP developers should know about 24 awesome PHP libraries (microframeworks)

18 how does HHVM improve PHP performance?

How to enable multi-process in 19php

20PHP Exception Handling

21PHP daemon instance

22PHP multi-thread programming-Analysis of pipeline communication instances

23PHP multi-process processing Parallel Processing Task instance

24PHP uses QPM to implement multi-process parallel task processing program

Multi-thread internal multi-thread instance analysis of 25 PHP

26xhprof Installation & Use

27 11 PHP frameworks recommended to developers

Differences between 28Cookie and Session-a good summary

29PHP built-in Session risks (blocking caused by session file exclusive lock)

How to compress and decompress strings in 30php

31php uses reflection to implement plug-in mechanism

Data Structure dual-chain table (spldoubly1_list) in the 32PHP SPL standard library)

33PHP SPL standard database-data structure fixed-length array (SplFixedArray)

34xss attack knowledge

SplQueue and SplPriorityQueue of 35PHP SPL standard library)

Streams in 36PHP

37php file system permission issues and solutions when running in fastCGI Mode

38PHP performance analysis tool XHProf installation tutorial

39 Baidu engineers explain the implementation principle and performance analysis of PHP functions (2)

40 Baidu engineers explain how PHP functions are implemented and Performance Analysis (3)

41 Baidu engineers explain the implementation principle and performance analysis of PHP functions (I)

42 correct Regular Expressions for PHP matching UTF-8 Chinese

Summary of socket series functions in 43php

SOCKET programming in 44PHP

45. How to hide the PHP version number on a Linux Server

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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