Home >Development Tools >composer >Simple installation steps and usage of php composer

Simple installation steps and usage of php composer

藏色散人
藏色散人forward
2022-01-12 16:07:476173browse

This article is provided by the composer tutorial column to introduce you to the concise installation steps for installing php composer. I hope it will be helpful to friends in need!

1. Introduction

Composer is a dependency management tool for PHP . It allows you to declare code libraries that your project depends on and it will install them for you in your project.

2. System requirements

PHP 5.3.2 or above is required to run Composer.
Composer is multi-platform, it can run on Windows, Linux and OSX platforms at the same time.

3. Installation (ubuntu)

curl -sS https://getcomposer.org/installer | php mv composer.phar
mv composer.phar /usr/local/bin/composer
Note: If curl is not installed, you can install it through the following command
apt-get update
apt-get install curl

After your Composer is installed, you can use the following Command to see if the installation is successful

composer -v
Note If the above method fails for some reason, you can also download the installer through 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 the Composer binary. 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 Composer installation directory through the --install-dir option (it can be an absolute or relative path)

4. Use

To start using Composer in your projects, you only need a composer.json file. This file contains the project's dependencies and other metadata.

First create a composer.json file, write the corresponding package name and version number, such as

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

After this, a dependent package is written, and then installed Dependency package. Get the defined dependencies to your local project, and then use Composer to run the install command in your project directory (that is, the directory where composer.json is located).

composer install

Of course, if you are in a Windows system, you can also install dependent packages by calling the composer.phar package.

php composer.phar install

Execute composer install to enter the automatic installation. After the installation is completed, a composer.lock file will be generated, which contains a specific version number. This file is required. Submit it to version management together with composer.json.

Finally, when you need to update dependency packages, you can use the following command

composer update

If you only want to update some dependencies

composer update monolog/monolog

5. Automatic loading

For library autoloading information, Composer generates a vendor/autoload.php file. You can introduce it in the entry file of your project

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;
?>

This makes it easy for you to use third-party code. For example: if your project depends on monolog, you can start using the library like this and they will be automatically loaded.

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;

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

$log->addWarning('Foo');
?>

6.Packagist/Composer China Full Image

Due to wall problems, foreign images of Composer often cannot be installed normallyinstall, so it is recommended to use domestic ones Mirroring is used as follows

There are two ways to enable this mirroring service:

  • System global configuration: That is, add the configuration information to Composer's global configuration file config.json. For details, see "Method 1"
  • Add configuration information to the composer.json file of a project. For details, see "Method 2"

Method 1: Modify composer's global configuration file
Open the command line window (windows users) or console (Linux, Mac users) and execute the following command:

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

Method 2: Modify the composer.json configuration file of the current project:

Open the command line window (windows users) or console (Linux, Mac users) and enter In the root directory of your project (that is, the directory where the composer.json file is located), execute the following command:

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

The above command will be in the composer.json# in the current project. ## The mirror configuration information is automatically added at the end of the file (you can also add it manually):

"repositories": {
    "packagist": {
        "type": "composer",
        "url": "https://packagist.phpcomposer.com"
    }
}
7. Use autoload in Composer to automatically load the namespace

Composer can not only help you In addition to installing the required dependency packages, you can also implement the function of automatically loading the namespace. When the function libraries and class libraries we write ourselves need to be automatically loaded, we can achieve this through

composer.json. It is similar to spl_autoload_register() in php. In fact, if you look at the source code in Composer, you will see that its automatic loading function also uses the spl_autoload_register() function. . "For details, please see this article for detailed introduction"

We add the following code in

composer.json:

{
    "autoload": {
        "psr-4": {
            "Test\\": "test/",
            "Testtwo\\": "testtwo/"
        }
    }
}

这个配置文件中有一个 autoload 段,其中有个 《PSR-4》,psr-4 是一个基于 psr-4 规则的类库自动加载对应关系,只要在其后的对象中,以 ”命名空间“: “路径” 的方式写入自己的类库信息修改完成后,之后,在执行下列命令,即可完成自动加载。

composer dumpautoload
注: "psr-4": {"Test\\": "test/"} 中的 "test/" 路径为相对于  composer.json 的路径

这个时候,你就可以调用你自己编写的函数库或者类库了

<?php
require __DIR__ . &#39;/vendor/autoload.php&#39;;

$testClass = new \Test\Testclass();
?>
注:本文内容参考了《Composer 中文网》,后续还会更新 Composer 其它的实用功能

The above is the detailed content of Simple installation steps and usage of php composer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete