Home  >  Article  >  Backend Development  >  Use of PHP dependency management tool Composer

Use of PHP dependency management tool Composer

WBOY
WBOYOriginal
2016-08-08 09:30:471079browse

Today I saw an introduction to the dependency management tool under PHP, so I learned and tried it:

Environment: win7

1. Installation

1. Confirm that PHP has enabled the openssl module (used when using https URLs);

Modify the environment Variable path, add c:xamppphp

Open the command line, enter:

php -version

Display:


Use the cd command to change the current path to the project root directory, and then run the command to download and install:

php -r "readfile('http://getcomposer.org/installer');" | php
I am using the http protocol URL here. If you have curl, you can also use the following command: The official website of
curl -sS https://getcomposer.org/installer | php
suggested that you can also directly download the installation package Composer-Setup.exe. Unfortunately, I saw it too late and did not try it out.

Go to the project root directory, add the composer.bat text file, and execute it on the command line:

echo @php "%~dp0composer.phar" %*>composer.bat
Close and reopen the command line, enter the command:
composer -V
to see the output version information.

Second example, the project needs to use monolog, a library that outputs logs.

Create the composer.json file in the project, enter the content:

{
    "require": {
        "monolog/monolog": "1.0.*"
    }
}
Execute on the cmd command line:
composer install
The required URL may not be downloaded normally due to some reasons. The prompt content may be:
Failed to enable crypto
failed to open stream: operation failed

At this time you have to consider solving your own network problems.

After running successfully, the vendor folder will appear in the project folder.

Usage example:

<?php
require_once  &#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');
?>

3. Others

Update self:

composer self-update

The above introduces the use of the PHP dependency management tool Composer, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn