Install and use the dependency management tool Composer in PHP. dependencycomposer_PHP tutorial

Source: Internet
Author: User
Tags autoloader mongodb server php development environment couchdb
Install and use the dependencycomposer dependency management tool in PHP. Install and use the dependency management tool Composer in PHP. the dependencycomposerPHPComposer must be PHP5.3.2 + or later. There are several PHP dependency management tools (dependency) for installation and use, dependencycomposer

Install PHP Composer

System requirements:

Composer needs PHP5.3.2 + and above to run. Several sensitive PHP settings and compilation flags are also required, but the installer issues a warning when there is any incompatibility.

For example, PHP extension requires that php without-disable-phar be installed or re-compiled.

To install a software package from the source address, instead of simply compressing the file package, you will need to install the software package's version control tools, such as git, svn, or hg.

Composer is compatible with multiple platforms and runs on Windows, Linux, and OSX.

Error message of installation failure:

curl -sS https://getcomposer.org/installer | PHP
#! / usr / bin / env php
some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:

The phar extension is missing.
Install it or recompile php without --disable-phar

Your PHP (5.2.9) is too old, you must upgrade to PHP 5.3.2 or higher.

My local default development environment is PHP 5.2.9. When I try to install on PHP 5.2.9, I am prompted that the version is too low, and I need to upgrade to PHP 5.3.2 or higher.

So I downloaded XAMPP 1.7.7 usb lite version.

E: \ USB \ Dropbox \ phpstorm \ php53> E: \ USB \ xampp_177 \ php \ php.exe -v
PHP 5.3.8 (cli) (built: Aug 23 2011 11:50:20)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

The PHP environment under XAMPP 1.7.7 is 5.3.8. After confirming that my PHP environment is greater than or equal to PHP5.3.2.

PHP 5.3 install Composer

E: \ USB \ Dropbox \ phpstorm \ php53> curl -sS https://getcomposer.org/installer | E: \ USB \ xampp_177 \ php \ php.exe
#! / usr / bin / env php
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:

The openssl extension is missing, which will reduce the security and stability o
f Composer.
If possible you should enable it or recompile php with --with-openssl

Downloading ...

Composer successfully installed to: E: \ USB \ Dropbox \ phpstorm \ php53 \ composer.phar
Use it: php composer.phar

note:

I turned off the SSL module of XAMPP 1.7.7 Apache, so Composer prompts me to warn, suggesting that I turn on the SSL module for security reasons. Does not turn on nor affect use.
Parameters during installation | PHP, be sure to change to the actual PHP environment path, for example | E: \ USB \ xampp_177 \ php \ php.exe
Use Composer as the PHP path + command. In our installed environment, the method is: E: \ USB \ xampp_177 \ php / php.exe composer.phar command.
The environment I installed is under Windows, if it is under Linux, and the PHP 5.3 environment is not under the default PHP command, it should be used |
curl -sS https://getcomposer.org/installer | / usr / php / 53 / usr / bin / php
After the installation is complete, check the installed version.

E: \ USB \ Dropbox \ phpstorm \ php53> E: \ USB \ xampp_177 \ php / php.exe composer.phar -V
Composer version 0209bd31a0ac3aeb2a68fc81e2d03c71072bef33

Check PHP Composer command:

E: \ USB \ Dropbox \ phpstorm \ php53> E: \ USB \ xampp_177 \ php / php.exe composer.phar
  ______
 / ____ / ___ ____ ___ ____ ____ ________ _____
// / __ \ / __ `__ \ / __ \ / __ \ / ___ / _ \ / ___ /
/ / ___ / / _ / / / / / / / / _ / / / _ / (__) __ / /
\ ____ / \ ____ / _ / / _ / / _ / .___ / \ ____ / ____ / \ ___ / _ /
          / _ /
Composer version 0209bd31a0ac3aeb2a68fc81e2d03c71072bef33

Usage:
 [options] command [arguments]

Options:
 --Help -h Display this help message.
 –Quiet -q Do not output any message.
 –Verbose -v Increase verbosity of messages.
 –Version -V Display this application version.
 –Ansi Force ANSI output.
 –No-ansi Disable ANSI output.
 --No-interaction -n Do not ask any interactive question.
 --Profile Display timing and memory usage information
 --Working-dir -d If specified, use the given directory as working directory
.

Available commands:
 about Short information about Composer
 archive Create an archive of this composer package
 config Set config options
 create-project Create new project from a package into given directory.
 depends Shows which packages depend on the given package
 diagnose Diagnoses the system to identify common errors.
 dump-autoload Dumps the autoloader
 dumpautoload Dumps the autoloader
 help Displays help for a command
 init Creates a basic composer.json file in current directory.
 install Installs the project dependencies from the composer.lock file
if present, or falls back on the composer.json.
 list Lists commands
 require Adds required packages to your composer.json and installs the
m
 run-script Run the scripts defined in composer.json.
 search Search for packages
 self-update Updates composer.phar to the latest version.
 selfupdate Updates composer.phar to the latest version.
 show Show information about packages
 status Show a list of locally modified packages
 update Updates your dependencies to the latest version according to
composer.json, and updates the composer.lock file.
 validate Validates a composer.json

In this way, Composer has been installed in PHP5.3 environment.

We can also install PHP 5.4 in a similar way.

PHP 5.4 install Composer

Download XAMPP 1.8.1 usb lite version, you will get PHP5.4.7 environment.

E: \ USB \ Dropbox \ phpstorm \ php53> E: \ USB \ xampp_181 \ php \ php.exe -v
PHP 5.4.7 (cli) (built: Sep 12 2012 23:48:31)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

After confirming that the version is higher than PHP 5.3.2, start the installation:

E: \ USB \ Dropbox \ phpstorm \ php54> curl -sS https://getcomposer.org/installer | E: \ USB \ xampp_181 \ php \ php.exe
#! / usr / bin / env php
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:

The openssl extension is missing, which will reduce the security and stability o
f Composer.
If possible you should enable it or recompile php with --with-openssl

Downloading ...

Composer successfully installed to: E: \ USB \ Dropbox \ phpstorm \ php54 \ composer.phar
Use it: php composer.phar

After the installation is complete, check the installed version.

E: \ USB \ Dropbox \ phpstorm \ php54> E: \ USB \ xampp_181 \ php / php.exe composer.phar -V
Composer version 0209bd31a0ac3aeb2a68fc81e2d03c71072bef33

In this way, Composer under PHP 5.4 environment is also successfully installed.

PHP Composer use

Here I only briefly demonstrate the installation of Symfony framework using PHP Composer.

Let's first explain the standard format of the command:

php composer.phar create-project symfony / framework-standard-edition path / 2.2.1

The first parameter PHP is the local PHP development environment, which points to the PHP run command.
The second parameter is the composer.phar download.
The third parameter is an action command, such as create-project. The purpose is to create a new project from a package to a specified directory.
The fourth parameter is the local installation directory.
The fifth parameter is the version of the installation package.

According to the Composer we installed in the PHP5.4 environment, our installation command is:

E: \ USB \ Dropbox \ phpstorm \ php54> E: \ USB \ xampp_181 \ php / php.exe composer.phar create-project symfony / framework-standard-edition E: \ USB \ xampp_181 \ htdocs \ Symfony 2.2.1

Installation results:

Installing symfony / framework-standard-edition (v2.2.1)
 – Installing symfony / framework-standard-edition (v2.2.1)
  Downloading: 100%
Created project in E: \ USB \ xampp_181 \ htdocs \ Symfony
Loading composer repositories with package information
Installing dependencies from lock file
 – Installing doctrine / lexer (v1.0)
  Downloading: 100%
 – Installing doctrine / annotations (v1.1)
  Downloading: 100%
 – Installing doctrine / cache (v1.0)
  Downloading: 100%
 – Installing doctrine / collections (v1.1)
  Downloading: 100%
 – Installing twig / twig (v1.12.2)
  Downloading: 100%
 – Installing psr / log (1.0.0)
  Downloading: 100%
 – Installing doctrine / inflector (v1.0)
  Downloading: 100%
 – Installing doctrine / common (2.4.0-RC1)
  Downloading: 100%
 – Installing symfony / symfony (v2.2.1)
  Downloading: 100%
 – Installing jdorn / sql-formatter (v1.2.0)
  Downloading: 100%
 – Installing doctrine / dbal (2.3.3)
  Downloading: 100%
 – Installing doctrine / doctrine-bundle (v1.2.0-beta1)
  Downloading: 100%
 – Installing doctrine / orm (2.3.3)
  Downloading: 100%
 – Installing jms / cg (1.0.0)
  Downloading: 100%
 – Installing phpoption / phpoption (1.2.0)
  Downloading: 100%
 – Installing jms / parser-lib (1.0.0)
  Downloading: 100%
 – Installing jms / metadata (1.3.0)
  Downloading: 100%
 – Installing jms / aop-bundle (1.0.0)
  Downloading: 100%
 – Installing jms / di-extra-bundle (1.3.0)
  Downloading: 100%
 – Installing jms / security-extra-bundle (1.4.0)
  Downloading: 100%
 – Installing sensio / distribution-bundle (v2.2.1)
  Downloading: 100%
 – Installing sensio / framework-extra-bundle (v2.2.1)
  Downloading: 100%
 – Installing sensio / generator-bundle (v2.2.1)
  Downloading: 100%
 – Installing kriswallsmith / assetic (v1.1.0-alpha4)
  Downloading: 100%
 – Installing symfony / assetic-bundle (v2.1.2)
  Downloading: 100%
 – Installing monolog / monolog (1.4.1)
  Downloading: 100%
 – Installing symfony / monolog-bundle (v2.2.0)
  Downloading: 100%
 – Installing swiftmailer / swiftmailer (v4.3.0)
  Downloading: 100%
 – Installing symfony / swiftmailer-bundle (v2.2.0)
  Downloading: 100%
 – Installing twig / extensions (v1.0.0)
  Downloading: 100%

kriswallsmith / assetic suggests installing leafo / lessphp (Assetic provides the integration with the lessphp LESS compiler)
kriswallsmith / assetic suggests installing leafo / scssphp (Assetic provides the integration with the scssphp SCSS compiler)
kriswallsmith / assetic suggests installing leafo / scssphp-compass (Assetic provides the integration with the SCSS compass plugin)
kriswallsmith / assetic suggests installing ptachoire / cssembed (Assetic provides the integration with phpcssembed to embed data uris)
monolog / monolog suggests installing doctrine / couchdb (Allow sending log messages to a CouchDB server)
monolog / monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog / monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog / monolog suggests installing mlehner / gelf-php (Allow sending log messages to a GrayLog2 server)
monolog / monolog suggests installing raven / raven (Allow sending log messages to a Sentry server)
Generating autoload files
Clearing the cache for the dev environment with debug true
Installing assets using the hard copy option
Installing assets for Symfony \ Bundle \ FrameworkBundle into web / bundles / framework
Installing assets for Acme \ DemoBundle into web / bundles / acmedemo
Installing assets for Sensio \ Bundle \ DistributionBundle into web / bundles / sensiodistribution

From the command line results, we saw the installation process and results of the entire Symfony framework.

PHP Composer defines dependencies

Composer will automatically find the composer.json file when using it. composer.json will be used to define the dependency package for the entire project. E.g:

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

Dependency package brief description, our project needs some monolog / monolog packages of any version starting from 1.2.

Composer is a dependency management tool in PHP. Through Composer, we can save a lot of files and control the version of dependent packages when publishing software.

PHP Composer project

Composer project official: http://getcomposer.org

Composer Github project: https://github.com/composer/composer

List of PHP Composer packages: https://packagist.org/


Under wamp, install the composer setup prompting The openssl extension is missing

No, change the integration package phpstudy. openssl is already configured by default. phpstudy extension management is very convenient, you can check it when you use it. . Support php5.2 to 5.6 one-click switching
 
When yii2 is installed using composer, the following error appears, heroes help, ...

You do n’t have mongodb extension enabled in PHP. It ’s okay with Composer.

Open your php.ini and enable mongodb extension (remove the ";" sign in front)

Restart your server (apache, iis ...)

And try again

If it's a linux system, I don't know. Search for it

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.