Later than other languages, PHP finally won its dependency manager. Composer makes life easier for developers by simplifying the management of packages and dependencies of your application. What was needed to be done manually or through the PERL or PECL can now be done using Composer. Simply create a file defining which packages your application needs to run and Composer does the rest for you.
Whenever we develop an application, we reuse other libraries to speed up our work. Some libraries can be downloaded and added to your project. Others can be installed globally with PERL or PECL. In both cases we have a problem: If on one hand we need to manually manage the libraries, on the other we have to content ourselves with the same version of the extension in all projects from that server. These are problems that Composer intends to solve.
Let's start with the installation. It is quite simple and can be done locally or made available for all your projects.
The installation on Unix-based operating systems is quite simple. As Composer needs only one file, it can be installed only for the project you want:
curl -sS https://getcomposer.org/installer | php
This will add Composer in your current directory. The file name is composer.phar and runs using the command php composer.phar. To simplify your life, you can move this file to the folder /usr/local/bin making Composer available for all your projects.
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Now you can run Composer by simply using the command composer. It may be necessary to use sudo when you move the file depending on your users' permissions.
If you use OSX, you can use homebrew to do the installation. Follow the steps provided in https://getcomposer.org/doc/00-intro.md#globally-on-osx-via-homebrew-.
On Windows the easiest way to install is to use the file Composer-Setup.exe. Download the file and run. The installer will put Composer in the PATH of your system, allowing you to use the command composer directly into the directory of your project (from the command line).
If an error such as "Composer Error [ERR_DOWNLOAD]" occurs, it must be because PHP is not in the Windows PATH or because the php_openssl is not active. To correct the PATH go to Control Panel > System > Advanced Settings. Then got to the Advanced tab and click Environment Variables. Select PATH, click Edit and include at the end the path to your PHP installation (for example c:xamppphp). In order to correct the php_openssl, simply open the php.ini, add "extension=php_openssl.dll" in the extensions section.
"description": "Description of my project.",
"keywords": ["projeto", "magazine"],
This is a composer.json file example. All the keys of this file are optional, but it only makes sense to use Composer if you fill the section require – these are the dependencies of your application. The other information is important if you plan to distribute your application as a package. Next, a brief explanation of each key:
- name: it's the name of the package. Usually developers use the address of the repository on Github, with the name of the owner first and the repository name after the "/".
- description: brief description of your package and what it does. Later you can create a README file with more information.
- keywords: key words, useful to help people finding your package.
- license: license type that you will use to distribute your package. Learn more about licenses in https://en.wikipedia.org/wiki/Free_software_license.
- require: here you will define what is needed in your application. You need to define the name of the package and the version you need. You can use "*", ">", "<", etc, besides requiring the branch name directly using "dev-master", "dev-develop", etc. However, in order for you to request a branch directly, you need the line "minimum-stability": "dev". More info at https://getcomposer.org/doc/01-basic-usage.md#package-versions.
- require-dev: this key is almost identical to require. The difference is that the packages defined here will be loaded only in the development environment, when we use the option –dev.
- autoload: definition of which classes should be automatically loaded in the autoload generated by Composer. This is the definition of the standard Laravel autoload.
- minimum-stability: as I commented before, this key lets you use branches instead of the versions when requesting packages.
Right after you create the file composer.json (which must be created in the root of your project), you must run the command:
This command will download all dependencies and put the files inside the vendor folder. In order to use the required libraries, you just need to use require 'vendor/autoload.php'; in your PHP code. This command will also create a file called composer.lock. In this file will be defined exactly what versions have been downloaded. It is recommended that you commit this file in your version control, this way other people working on the same project will get exactly the same versions when running composer install.
When you need to update the versions of your dependencies, you can run the command:
This way Composer will update the versions and also the file composer.lock.
If you use a version control system, you should ignore the vendor folder. Other people working on the same project will download these files using composer install, so there's no need to version this folder.
It took a while but finally the PHP developers have their dependency manager. Composer brings a number of benefits and facilitates the life of the developer. So, it is worth to install and learn to use this tool. And if you want to work with a modern framework such as Laravel, Composer is a requirement.