PHP: What is Composer, and How to use it

January 29, 2022

Table of Contents


What is Composer

Composer is a package manager for PHP.
With Composer we can install third party libraries automatically.

If you have been using PHP for a while, you would remember that in the past the usual way to install third party libraries was to download a zip file, extract its content, and place it in a location your project would have access to. After that, you would have to include in your code either with a include or require instruction.

Several times, you may needed to use several third party libraries which would have required to download several packages from diferent sources, and include all of them in your code.

Composer would make the installation of third party libraries much easier and faster. Composer would download the needed files of the third party library, as well as any require dependencies

You can find more information about Composer on its website https://getcomposer.org/


Installation

The installation is performed with the following command on your project folder:


curl -sS https://getcomposer.org/installer | php

After the installation is complete the composer.phar file would have been created on your folder.


How to use it

To use composer to install php packages, there two options. You can use the Composer require command, or you can create composer.json file, and use the Composer update command.


  • Using "composer require"

    To use the Composer require command use it as follows, with name of the package you want to install:

    
    php composer.phar require <package>
    

    For example, if we want to install the Symfony Mailer package, we would execute the following command:

    
    php composer.phar require symfony/mailer
    

  • Using the composer.json file and "composer update"

    The second option, is to create a file named composer.json

    For example, to install the Symfony Mailer package, you would put the following content on the composer.json file.

    
    {
       "require": {
          "symfony/mailer": "^5.4"
       }
    }
    

    After that, you would have to run the Composer update command.

    
    php composer.phar update
    

  • Sample PHP code

    Now that we have the package installed, we can create our php file.

    On the file, we will add the following file to include the package, so that we can use it on our script.

    
    require_once __DIR__ . 'vendor/autoload.php';
    

    Next, we can add the code that uses the package.

    
    <?php
    require_once __DIR__ . 'vendor/autoload.php';
    
    use Symfony\Component\Mailer\Mailer;
    use Symfony\Component\Mailer\Transport\SendmailTransport;
    use Symfony\Component\Mime\Email;
    
    $transport = new SendmailTransport();
    $mailer = new Mailer($transport);
    
    $email = (new Email())
        ->from('sender@example.com')
        ->to('recipient@example.com')
        ->subject('Sample message')
        ->text('This is a sample message')
        ->html('This is a sample message');
    
    $mailer->send($email);