Home > Article > Backend Development > Use of PHP dependency management tool Composer
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
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');" | phpI 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 | phpsuggested 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.batClose and reopen the command line, enter the command:
composer -Vto 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 installThe 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
After running successfully, the vendor folder will appear in the project folder.
Usage example:
<?php require_once 'vendor/autoload.php'; $log = new Monolog\Logger('name'); $log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING)); $log->addWarning('Foo'); ?>
3. Others
Update self:
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.