Articles

What is Composer for PHP, features and how to use it

Composer is an open source, dependency management tool for PHP, primarily created to facilitate the deployment and maintenance of PHP packages as individual application components.

The Composer radically changed the PHP ecosystem, creating the basis for the evolution of modern PHP, i.e. component-based applications and frameworks.

Features

The requirements are declared in a project-level JSON file, which Composer then uses to evaluate which package versions best match the application's dependencies. The assessment will consider nested dependencies and system requirements, if any.

It is important to note that Composer allows you to install the necessary libraries on a per-project basis. It allows you to use different versions of the same library on different PHP projects.

To install and use libraries managed by Compose, you will have to declare them in the project in a standard format and Composer will take care of the rest. For example, if you want to install the mpdf library using Composer, you need to run the following command in your project root.

$composer require mpdf/mpdf

But where does the Composer download the libraries from?

what libraries are available ?

There is a central repository where Compose keeps a list of available libraries: the Packagist.

Installation

Now let's see how to install Composer on operating systems such as Linux, macOS and Windows.

Installation – Linux / Unix / maxOS

To install composer on linux, unix and macOS, you need to download the installer at https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos and install it locally as part of your project or globally as a system-wide executable.

The installer will check some PHP settings, and download a file called composer.phar into your working directory. This is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP that can be executed from the command line, among other things.

php composer.phar
Installation – Windows

To install composer on Windows, you need to download the installer at https://getcomposer.org/doc/00-intro.md#installation-windows

Once the installation is complete, you can verify that it works correctly with the command

Innovation newsletter
Don't miss the most important news on innovation. Sign up to receive them by email.
composer -V

and you should have an answer like this

Packagist

Packagist, the public repository of Compose, contains a collection of PHP libraries open source made freely available through Composer. A premium version of the service offers hosting for private packages, making it possible to use Composer even on closed source projects.

There are hundreds of libraries available on Packagist, which shows the popularity of Composer. In your PHP projects, if you need a feature that you think should already be available as a third-party library, Packagist is the first place you should look.

In addition to the Packagist, you can ask Composer to look at other repositories for library installation by changing the repositories key in the composer.json file. In fact, this is what you'll do if you want to manage your private Composer packages.

How to use composer

There are two ways to install libraries with Composer. Let's see them both:

The install command

To use the installer, you must first create a composer.json file in your project. In the composer.json file, you just need to declare your project's dependencies, as shown in the snippet below.

{
    "require": {
        "mpdf/mpdf": "~6.1"
    }
}

Later, when you run the composer install command, in the same folder where the json file is, Composer installs the mpdf package and its dependencies in the vendor directory.

The require command

We can say that the composer require command is a kind of shortcut to perform the previous process of creating a composer.json file. require will automatically add a package to your composer.json file. The following command shows how to install the mpdf package with the help of require.

$composer require mpdf/mpdf

After installing the mpdf package and its dependencies, require also adds an entry of the package being installed in the composer.json file. If the composer.json file doesn't exist, it will be created on the fly.

Ercole Palmeri

Innovation newsletter
Don't miss the most important news on innovation. Sign up to receive them by email.

Latest Articles

Veeam features the most comprehensive support for ransomware, from protection to response and recovery

Coveware by Veeam will continue to provide cyber extortion incident response services. Coveware will offer forensics and remediation capabilities…

April 23 2024

Green and Digital Revolution: How Predictive Maintenance is Transforming the Oil & Gas Industry

Predictive maintenance is revolutionizing the oil & gas sector, with an innovative and proactive approach to plant management.…

April 22 2024

UK antitrust regulator raises BigTech alarm over GenAI

The UK CMA has issued a warning about Big Tech's behavior in the artificial intelligence market. There…

April 18 2024

Casa Green: energy revolution for a sustainable future in Italy

The "Green Houses" Decree, formulated by the European Union to enhance the energy efficiency of buildings, has concluded its legislative process with…

April 18 2024