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

Use of PHP dependency management tool Composer

WBOY
Release: 2016-08-08 09:30:47
Original
1089 people have browsed it

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
Copy after login

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
Copy after login
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
Copy after login
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
Copy after login
Close and reopen the command line, enter the command:
composer -V
Copy after login
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.*"
    }
}
Copy after login
Execute on the cmd command line:
composer install
Copy after login
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
Copy after login

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');
?>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template