Composer quick start

Source: Internet
Author: User
What is Composer?

Composer is a dependency management tool for PHP. You can declare your dependent libraries in your project, and then Composer will help you solve the following problems: find these libraries and the versions that can be installed in these libraries, and then install them. Therefore, Composer is a dependency management tool, rather than a package management tool (similar to Yum or Apt), because it manages these packages based on each project, install these packages to a directory in the project.

Install Composer

Composer requires PHP 5.3.2 + and some PHP configurations. if incompatibility occurs, a prompt will be prompted during the installation process. There are two ways to install Composer. one is local installation, but global installation.

Local installation

The local installation installs Composer in the current directory. Run the following command to install:

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '070854512ef404f16bac87071a6db9fd9721da1684cd4589b1196c3faf71b9a2682e2311b36a5079825e155ac7ce150d') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"$ php composer-setup.php$ php -r "unlink('composer-setup.php');"

There are a total of four commands that perform the following operations:

  1. Download the installation file to the current directory.

  2. Verify the SHA-384 hashes of the installation file.

  3. Run the installation file.

  4. Delete the installation file.

Then we can run the following command to run Compoer:

$ php composer.phar

Note that, in step 2, check the file hash. the hash values of each version of the installation file are different. Therefore, you are advised to go to the download page to obtain the installation code. If you do not think it is necessary to check the installation file, you can skip this command to execute the following command. Of course, we do not recommend this from the security perspective.

Global Installation

Global installation is to install Composer to a directory in the PATH variable, so that you can access Composer from anywhere. I personally suggest doing the same. We only need to move the locally installed composer execution file to the global directory:

$ mv composer.phar /usr/local/bin/composer

In this way, you can directly run the composer command anywhere. Unless otherwise specified, commands are executed globally.

Installation options

Composer supports three options during installation.

-- Install-dir

You can use the -- install-dir option to modify the installation path of Composer. for example, if you want to install Composer in the bin directory:

$ php composer-setup.php --install-dir=bin
-- Filename

With the -- filename option, we can modify the name of the Composer execution File (composer. phar by default ). For example:

$ php composer-setup.php --filename=composer
-- Version

To install the specified version of Composer, you can use the -- version option:

$ php composer-setup.php --version=1.0.0-alpha8
Manual Download

In addition to the above installation method, you can also directly download the execution file. Please download it on the download page.

Composer update

Updating Composer is simple. you only need to execute the following command:

$ Composer selfupdateUpdating to version 1.1.2 (stable channel). Downloading: 100% Use composer self-update -- rollback to return to version updated # or (the two are equivalent) $ composer self-update

To perform version rollback, run the following command:

$ composer selfupdate --rollbackRolling back to version 2016-05-26_16-11-16-06c4562.
The basic usage of Composer is the composer. json file.

After installing Composer, you only need to create a composer. json file to start using it in the project. This file describes the packages that your project depends on and some other metadata.

Require key

With the require configuration item, we can specify the project dependency. Suppose our project needs to use the monolog/monolog logstore, then we can configure the composer. json file as follows:

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

The require value is an object. each key in the object corresponds to a dependency. The key name is the package name and the key value is the package version.

Package name

A package name consists of a vendor name and a project name. This ensures the uniqueness of the package name. The project name can be repeated, but the vendor name varies with each other. Taking monolog/monolog as an example, the vendor name and project name are both monolog.

Package Version

In the preceding example, the version of monolog/monolog is required to be 1. 0. *, indicating that any development branch version of 1.0 meets the requirements. There are many methods to specify a version, which will be explained in detail in later articles.

Install dependency

After creating the composer. json file and configuring require, we can install dependencies by running the following command:

$ composer installLoading composer repositories with package informationUpdating dependencies (including require-dev)  - Installing monolog/monolog (1.0.2)    Downloading: 100%         Writing lock fileGenerating autoload files

Composer downloads the latest version of monolog/monolog to the default directory of vendor according to the version conventions configured above.

Composer. lock file

After running the above install command, you will find that there will be an additional composer. lock file in addition to the vendor directory. This file stores the version of each package that has been installed in the project. When running the install command, if this file exists, Composer will download the corresponding package version based on this file. The advantage is that the dependent versions of each environment can be consistent. Otherwise, if this file is not available, the versions that may be downloaded during install may be different. Therefore, we recommend that you put the composer. lock file in version control.

Update dependency

To update dependencies, run the update command:

# Update all dependencies $ composer update # update a dependency $ composer update monolog/monolog
Automatic loading

After downloading the dependency, we can start to use the library provided by the dependency. Composer will create an automatically loaded file named vendor/autoload. php for the downloaded library. we only need to include this file to easily call the functions of each library. Take monolog/monolog as an example:

$log = new Monolog\Logger('name');$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));$log->addWarning('Foo');

We don't need to worry about the loading of library files. the Composer autoload. php file has helped us handle the automatic loading of each database.

Packagist image

So far, we have installed Composer and learned its basic usage, so we can play happily. However, it is well known that for some reason, github and packagist may be inaccessible or slow in China, which may lead to various kinds of unhappiness when using Composer. Fortunately, we have an image in China to solve this problem. you only need to change the repository path to the image path.

There are two ways to modify the global configuration of Composer (recommended ):

$ composer config -g repo.packagist composer https://packagist.phpcomposer.com

This command modifies Composer's global configuration file config. json. Second, modify the configuration of a single project:

$ composer config repo.packagist composer https://packagist.phpcomposer.com

This command will modify the composer. json file under the project and add the following configuration information:

"repositories": {    "packagist": {        "type": "composer",        "url": "https://packagist.phpcomposer.com"    }}

Of course, you can also manually modify the composer. json file to add the above configuration information.

For details, visit http://pkg.phpcomposer.com /.

Reference
  • Https://getcomposer.org/

  • Http://pkg.phpcomposer.com/

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.