Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
Composer deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project.
Let’s say you are creating a project, and you need a library that does logging. You decide to use monolog. In order to add it to your project, all you need to do is create a composer.json file which describes the project’s dependencies.
{ "require": { "monolog/monolog": "1.2.*" } }
We are simply stating that our project requires some monolog/monolog package, any version beginning with 1.2.
Composer requires PHP 5.3.2+ to run.
Installing Composer
Installing Composer is really easy as it can all be done through the command line. I’m using OS X, but the following should be the same for any *nix operating system.
$ curl -s https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer
The first command downloads the composer.phar file to your computer. The second line moves the composer.phar file in to your bin so that is accessible globally on your computer.
$ composer
If you have installed Composer successfully, you should be given a list of available commands and descriptions.
Now that you have Composer installed, you can use it to require some packages into your project. To make a Composer configuration file, we just need to make a JSON file in the root of the project.
For example, if you wanted to use monolog package you could create the following composer.json file.
{ "require": { "monolog/monolog": "1.2.*" } }
To resolve and download dependencies, run the install command:
$ php composer.phar install
If you did a global install and do not have the phar in that directory run this instead:
$ composer install
Following the example above, this will download monolog into the vendor/monolog/monolog directory.
Besides downloading the library, Composer also prepares an autoload file that’s capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code’s bootstrap process:
require ‘vendor/autoload.php’;
That’s it, enjoy the new gained knowledge
The AI research landscape is currently one of the most dynamic and vibrant fields, showing no signs of slowing down…
In the dynamic landscape of technology, cloud computing emerges as the linchpin of innovation. Did you know the cloud computing…
Did you know thе sеcrеt bеhind Ubеr's ability to connеct drivеrs and ridеrs quickly and еfficiеntly? The answer is Michaеlangеlo!…
DevOps, the buzzword of yesteryears, is a concrete reality in forward-moving enterprises today. Organizations are actively adopting DevOps practices to…
Cloud computing and Artificial Intelligence (AI) are two fundamental pillars that are driving businesses forward in numerous ways beyond the…
Cloud computing has revolutionized the way businesses operate by providing a highly scalable, flexible, and cost-effective way to manage IT…