Composer is a dependency manager for PHP. It makes your life easy. If you are a Java developer and have used Apache Maven, you’ll find Composer a familiar tool. Unlike Maven, dependencies are declared in a file called composer.json which is normally placed in the root directory of a PHP project. All dependencies are, by default, downloaded into the /vendor subdirectory from https://packagist.org. In Apache Maven, files are coming from Maven Central (default).
It is also worth mentioning that the downloaded dependencies also carry with them their own composer.json file. So although you have one dependency in your composer.json file, that dependency may have dependencies on other libraries as well.
To start using the libraries, just include ‘vendor/autoload.php’ to your application’s bootstrap process. These are just a few things you’ll encounter in Composer but many of the underlying concepts and principles used are similar to that used or implemented in Maven (except the more advanced stuff).
Definitely, Composer is not Apache Maven. PHP is another world.
- Windows 7 Professional SP1
- PHP 5.6.3 / Zend Engine v2.6.0
- Composer v1.0-dev
Installing and Using Composer
You need to have PHP installed in your system. I will be using the PHP interpreter that comes with XAMPP.
Set Path to PHP
Open Windows command prompt and change directory to some new directory. Set the PATH to the PHP directory within your XAMPP home directory. Then, verify if PHP works.
Composer can be downloaded/installed using a PHP file function invoked on the command line.
Once the installation is complete, you’ll have composer.phar and that’s all you need.
Determine the Composer’s version
It is easy to find out the Composer’s version. Using composer.phar, simply pass –version as its parameter.
Your Dependency File
As mentioned before, dependencies are declared in a file named composer.json. Here, we have a sample file that declares dependency to “ollieread/multiauth”.
Then, you will have two files in your current directory.
Install the Dependencies
In Apache Maven, you would invoke “mvn install”. Here, you’ll use “php composer.phar install”.
As mentioned earlier, the libraries you reference in your composer.json may also have dependencies on other libraries. Composer will perform cascade install starting from your application’s dependency. For instance, although we referenced “ollieread/multiauth” as our direct dependency, the library also has dependencies on other libraries.