Installing composer
Composer requires PHP 5.3.2+ to run.
-sS https://getcomposer.org/installer | php
This command will be composer.phar downloaded to the current directory. PHAR (PHP compression package) is a compressed format that can be run directly under the command line.
You can use the --install-dir option to install composer to the specified directory, for example:
-sS https://getcomposer.org/installer | php -- --install-dir=bin
Of course, you can also do a global installation:
-sS https://getcomposer.org/installer | php$ mv composer.phar /usr/local/bin/composer
Claim dependent
Create a file in the project directory composer.json that indicates dependencies, for example, your project relies on Monolog:
{ "require": { "monolog/monolog": "1.2.*" }}
Installation dependencies
Installation dependencies are simple, just run in the project directory:
composer install
If there is no global installation, then run:
php composer.phar install
Auto Load
Composer provides an auto-load feature that simply includes the following line in the initialization section of your code:
require ‘vendor/autoload.php‘;
Module Warehouse
Packagist.org is a warehouse of composer, where many well-known PHP libraries can be found. You can also submit your own work.
Advanced Features
The above describes the basic usage of composer. Composer also has some advanced features that, although not required, can often be handy for PHP development.
How to use
<?php require __dir__. '/vendor/autoload.php '; Use Monolog\logger; Use Monolog\handler\streamhandler; Use Monolog\handler\bufferhandler; Use Monolog\errorhandler; Use Monolog\processor\memoryusageprocessor; $logger = new Logger (' Error_logger '); $stream = new Streamhandler (__dir__. ') /error.log ', logger::error); $logger->pushhandler (New Bufferhandler ($stream, logger::D Ebug, True, true));// Use Bufferhandler to set the number of logs in the same request up to 10 and write once again file errorhandler::register ($logger);
Require ' vendor/autoload.php '; $log = new Monolog\logger (' name '); $log->pushhandler (New Monolog\handler\ Streamhandler (' App.log ', monolog\logger::warning)); $log->addwarning (' Foo ');
Composer a new era of PHP dependency management