PHP composer use

Source: Internet
Author: User
Tags autoload autoloader composer install using git
composer-php for managing dependencies (dependency)

Defined

  1. Composer is a tool used in PHP to manage dependency (dependency) relationships. You can declare your dependencies on the External tool Library (libraries) in your project, and composer will help you install these dependent library files.
  2. Composer need to php5.3.2+ above the environment to run. There are several sensitive PHP settings and compilation flags that are required, but the installer warns you when there are any incompatibilities.
  3. The Composer is a multi-platform compatible operation for Windows,linux and OSX.

    Installation

  4. Linux

    • Curl-ss Https://getcomposer.org/installer | Php
    • You can specify the installation directory for Composer (which can be an absolute or relative path) by using the--install-dir option
    • Global Installation
      MV Composer.phar/usr/local/bin/composer
      Now just run the composer command to use composer without the need to enter PHP Composer.phar.
  5. Windows
    Download Https://getcomposer.org/Composer-Setup.exe
    Windows may need to set environment variables for composer
  6. Composer-v View version number
    Composer-help Viewing command Help
  7. Reference documents
    http://docs.phpcomposer.com/
  8. Composer use of Chinese mirrors
    http://pkg.phpcomposer.com/

    Composer.json File Description

  9. Require key

    {    "require": {        "monolog/monolog": "1.0.*"    }}
    • Require requires a package name (for example, Monolog/monolog) to map to the object of the package version (for example, 1.0.*).
    • The package name is comprised of the vendor name and its project name
  10. Version operators

    • Exact version 1.0.2
    • Wildcard 1.0.* all versions of 1.0
    • Range >, >=, <, <=,! =, you can define multiple ranges, separated by commas, which are treated as a logical and. A pipe symbol | will be processed as a logical OR.
    • The assignment operator ~1.2,~1.2 is equivalent to >=1.2,<2.0, allowing any version of more than 1.2, but excluding 2.0. You'll also see another usage of it, using ~ To specify the minimum version, but allow the last digit of the version number to rise.
  11. Perform installation of PHP Composer.phar install

    • Following the previous example, this will find the latest version of Monolog/monolog and download it to the vendor directory. It is a convention to put a third-party code into a specified directory vendor. If it is Monolog, the Vendor/monolog/monolog directory will be created.
    • The other thing is that the install command will create a composer.lock file to your project's root directory.
    • If you are using Git to manage your project, you may want to add vendor to your. gitignore file. You don't want to add all of your code to your repository.
  12. Composer.lock-Lock file

    • After the installation is dependent, Composer will write the exact version number list at the time of installation to the Composer.lock file. This locks the specific version of the project.
    • Use the update command. This will get the latest matching version (depending on your Composer.json file) and update the new version into the lock file. PHP Composer.phar Update
    • If you only want to install or update a dependency, you can whitelist them:
      PHP composer.phar update monolog/monolog [...]
  13. Auto Load

    • For the library's automatic loading information, Composer generates a vendor/autoload.php file. You can simply introduce this file and you will get a free auto-load support.
      Require ' vendor/autoload.php ';
    • You can add your own autoloader in the AutoLoad field of Composer.json.

      {    "autoload": {        "psr-4": {"Acme\\": "src/"}    }}
    • You can define a mapping from a namespace to a directory. At this point, SRC will be in the root directory of your project, with the vendor folder sibling. For example, the src/foo.php file should contain the Acme\foo class.
      After you add the AutoLoad field, you should run the install command again to generate the vendor/autoload.php file.
    • Referencing this file will also return an instance of Autoloader, where you can store the return value of the containing call in a variable and add more namespaces. This is useful for automatically loading class files in a test suite, for example.

      $loader = require 'vendor/autoload.php';$loader->add('Acme\\Test\\', __DIR__);

      Using the example

  14. Use of BASIC commands
  15. A minimized composer.json, switch to the project directory, create a new Composer.json

    {    "require-dev": {        "phpunit/phpunit": "4.3.*"    }}
  16. Execute composer Install

    • will create the vendor directory, which is the relevant code.
    • The Composer.lock file is created
    • Generates a PHP file that vendor/autoload.php is automatically loaded

The above describes the use of PHP composer, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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