If you are a web developer and love to use PHP, i bet you have heard about composer. Composer is dependency manager for PHP. So what is dependency means? In developing a web application you may need to use one or more libraries. In my case i often use these libraries: monolog, slim, laravel, debug helper, and laravel. Without composer it would take me hours just to do the environment setup. With composer i just need to define a composer.json file, and composer will handle the rest, download, compile all libraries and create the autoload. It is so convenient.
Install Composer on Windows
To install Composer on Windows is very easy, because composer come with the installer. You can download the installer form composer download page. After installation you can use composer from your command prompt or if you have git bash (msysgit) installed, you can use it from there as well. And if you have trouble to use composer from the command line, you may need to check your path variable in your environment variables.
Basic Use of Composer
To get all the list of commands available you can just type composer list. To use composer for your project you must define composer.json file, where you can put the information of your project and the required libraries. See the example below for the basic use of composer.json file:
{ "require": { "monolog/monolog": "dev-master" } }
And to run the installation, you only need to type composer install, and all the required libraries will be downloaded and autoload file will be created. Composer will place all the libraries into the vendor folder and then create the autoload file.
You can also use composer init, to generate more complex composer.json file, there will be an interactive and it is easy to use. And this is one of the example:
{ "name": "calibrefx/api", "description": "CalibreFx API Sustem.", "keywords": ["api", "calibrefx"], "require": { "laravel/framework": "4.0.6", "way/generators": "dev-master", "zizaco/confide": "dev-master", "zizaco/entrust": "dev-master" }, "autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ] }, "scripts": { "post-install-cmd": [ "php artisan optimize" ], "pre-update-cmd": [ "php artisan clear-compiled" ], "post-update-cmd": [ "php artisan optimize" ], "post-create-project-cmd": [ "php artisan key:generate" ] }, "config": { "preferred-install": "dist" }, "minimum-stability": "dev" }
The above code is for a laravel project, and it is all autogenerated. All i need is just execute this code:
composer create-project laravel/laravel your-project-name --prefer-dist
Then composer will automatically setup everything for me, saving a lot of my time.
Calling the autoload file in PHP
After composer download and create the autoload file for you, now you just need to call it once in your PHP code, see the example below:
require 'vendor/autoload.php';
The above code will call the autoload.php file generated by composer where you can access all the class and function that you have in your libraries.
And the good thing from composer is, there are a lot of libraries available online, you can search it in packagist.org