Composer is a tool that allows to manage your third party dependencies in your PHP application, it will allow you to create a list of all third party scripts and libraries and their version number. Composer will then look into the repository of the third party library and the current version number and will automatically download the library into your application
Installing Composer
Composer is bundled as an executable Phar archive, so make sure you have the Phar extension enabled in your php.ini file (uncomment extension=phar.so).
I recommend download the latest snapshot of the Composer executable directly from the project’s website.
Download Composer into this folder with the following command :
1 | curl -s https://getcomposer.org/installer | php |
You should see a success message that looks something like this:
1 2 3 4 5 | All settings correct for using Composer Downloading... Composer successfully installed to: /var/www/my_project/composer.phar Use it: php composer.phar |
Ultimately as a user, all you have to do is drop a composer.json file in your project and run composer.phar install. This composer.json file defines your project dependencies, and optionally configures composer (more on that later). Here is a minimal example to require one library:
1 2 3 4 5 | { "require": { "monolog/monolog": "1.0.0" } } |
You can declare it here (see the last two lines below) and Composer will generate an autoloader for the user that can load all of his project dependencies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | { "name": "monolog/monolog", "description": "Logging for PHP 5.3", "keywords": ["log","logging"], "homepage": "http://github.com/Seldaek/monolog", "type": "library", "license": "MIT", "authors": [ { "name": "Jordi Boggiano", "homepage": "http://seld.be" } ], "require": { "php": ">=5.3.0" }, "autoload": { "psr-0": {"Monolog": "src/"} } } |
Make sure you also check out the official Composer documentation for updates and techniques I couldn’t discuss in this article.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: Composer