Hello,
Today, I wanted to explain you how to use Composer.
First at all, one little definition:
What is Composer?
Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries. It was developed by Nils Adermann and Jordi Boggiano, who continue to manage the project.
Issued from Wikipedia
How to install Composer?
On Windows, you have to go to Composer website, to download it.
For experienced developers, we can install it from the command line.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'baf1608c33254d00611ac1705c1d9958c817a1a33bce370c0595974b342601bd80b92a3f46067da89e3b06bff421f182') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Or, through cURL :
$ curl -sS https://getcomposer.org/installer | php
Ou, through PHP :
$ php -r "readfile('https://getcomposer.org/installer');" | php
Then, you can start using it.
How to use Composer?
Composer has many parameters, very useful:
- require: Adds package to composer.json file and install it.
Launch this command:
composer require vendor/package
Will add the following line to the composer.json file:
{
"require": {
"vendor/package": "1.*"
}
}
And will install the required package files on vendor/package directory.
You also can specify the package version you want to install, like this:
composer require vendor/package:2.*
I personally recommend using the --prefer-stable option.
-
install: install all libraries from composer.json. It's the command to use to download all PHP repository dependencies.
-
update: update all libraries from composer.json, according to the allowed versions mentioned into it.
-
remove: uninstall a library and remove it from composer.json.
Find all the commands available on the Composer documentation.
What is the composer.json file ?
This is the file that stores all packages (dependencies) of a project, as well as their versions, but can also be used to define a project when it has been published on Git, for exemple.
This is what look like this file:
{
"name": "your-vendor-name/package-name",
"description": "A short description of what your package does",
"require": {
"php": "^7.2",
"vendor/package": "1.*",
"another-vendor/package": "1.*"
}
}
When composer.json file is used?
This file stores all the dependencies of a project, without having to physically install them in the directory vendor/, especially when stored on a version manager (like Git), which greatly reduces the size of the resources to be stored. The developer who wants to use a project, install dependencies, via a composer: install
or composer: update
.
How Composer works?
Composer is used to manage project dependencies, but it also allows to autoload all the required files for their use. This is what the vendor/autoload.php
file do.
require __DIR__ . '/vendor/autoload.php';
See composer autoloading documentation
How to add our code to autoloader?
By adding an autoload rule in the composer.json
file, like this:
{
"autoload": {
"psr-4": {"MyProjectName\\": "src/"}
}
}
How to use Composer with Symfony?
The Symfony Framework is a Composer project! It can be installed through this one, by command line, like this:
For Symfony version < 3.4 :
composer create-project symfony/framework-standard-edition my_project_name
For Symfony version > 4.0 :
composer create-project symfony/website-skeleton my_project_name
These commands will create a directory my_project_name
with all mandatory files to code a web application. See Symfony official documentation
SensioLabs developped packages, Symfony components will also can be singly installed, according to your needs! See Symfony packages on Packagist
Others third-party packages (formerly called Bundles) can also be installed through composer, see Bundles list on packagist
In order for the Framework to load these third-party packages, you also need to tell Symfony what to load:
For Symfony < 3.4 :
// app/AppKernel.php
public function registerBundles()
{
$bundles = [
new VendorName\Path\To\Bundle(),
]
return $bundles;
}
For Symfony > 4.0 :
// config/bundles.php
return [
// 'all' means that the bundle is enabled for any Symfony environment
VendorName\Path\To\Bundle::class => ['all' => true],
];
Note that since version 4, it is no longer advisable to organize the code into Bundles.
See you soon,
Mathieu