Composer
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will install and update them for you. We will learn:
- how to install Composer
- use it in new or existing project
Installation
Composer is an executable .phar
file that you download and install as follows.
Windows
Use the official installer Composer-Setup.exe.
Linux, macOS
All you need is 4 commands, which you can copy from this page.
Further more, by copying into folder that is in system's PATH
, Composer becomes globally accessible:
$ mv ./composer.phar ~/bin/composer # or /usr/local/bin/composer
Use in an existing project
To start using Composer in your project, all you need is a composer.json
file. This file describes the
dependencies of your project and may contain other metadata as well. The simplest composer.json
can look
like this:
{
"require": {
"nette/database": "^2.3"
}
}
We're saying here, that our application (or library) depends on package nette/database
(the package name consists
of a vendor name and the project's name) and it wants the version that matches the ^2.3
version constraint.
So, when we have the composer.json
file in the project root and we run:
composer update
Composer will download the Nette Database into directory vendor
. It also creates a composer.lock
file, which contains information about exactly which library versions it installed.
Composer generates a vendor/autoload.php
file. You can simply include this file and start using the classes that
those libraries provide without any extra work:
require __DIR__ . '/vendor/autoload.php';
$db = new Nette\Database\Connection('sqlite::memory:');
Creating New Project
New Nette project can be created by executing a simple command:
composer create-project nette/web-project name-of-the-project
Instead the name-of-the-project
you should provide the name of the directory for your project and execute the
command. Composer will fetch the nette/web-project
repository from GitHub, which already contains the
composer.json
file, and right after that install the Nette Framework itself. The only thing which remains is to check
write permissions on directories temp/
and log/
and you're ready to go.
PHP Version
Composer always installs those versions of packages that are compatible with the version of PHP you are currently using. Which,
of course, may not be the same version as PHP on your web host. Therefore, it is useful to add information about the PHP version
on the host to the composer.json
file, and then only versions of packages compatible with host will be installed:
{
"require": {
...
},
"config": {
"platform": {
"php": "5.6" # PHP verion on host
}
}
}
Packagist.org – Global Repository
Packagist is the main package repository, in which Composer tries to search packages, if not told otherwise. You can also publish your own packages here.
What If We Don't Want the Central Repository
If we have internal applications or libraries in our company, which cannot be hosted publicly on Packagist, we can create our own repositories for those project.
More on repositories in the official documentation.
Configuration
Composer is closely integrated with version control tool Git. If you do not use Git, it is necessary to tell it to Composer:
composer -g config preferred-install dist