Blog postPackagist and Composer

How to use Packagist and Composer to integrate existing code libraries into your PHP apps?

Updated August 28, 2015

Are you tired of having to reinvent the code in any project in which you work? Would you like to know where the PHP community stores all the code libraries that are written worldwide? Have you ever wondered where you can get a code library to manage your website users or to handle your shopping cart? Would you rather write less code and still achieve more?

While Packagist is a huge repository of PHP code libraries, Composer is a dependency manager which allows us to download the code libraries from Packagist straight to our project.

If you are a PHP programmer and answered yes to at least one of these questions, you should familiarize yourself with Packagist and Composer that allow us to easily find PHP libraries, download them to our website, and manage them as well. While Packagist is a huge repository of PHP code libraries, Composer is a dependency manager which allows us to automatically download the code libraries from Packagist straight to our project along with all of their dependencies (the code packages on which they are dependent).

How to install composer?

Installing Composer on Linux and Mac is done via the command line and is pretty damn easy:

$ curl -s https://getcomposer.org/installer | php

As you can see in the following link: getcomposer

Installing Composer on Windows machines is also not particularly difficult using a dedicated Wizard (see the documentation).

We examine the installation by typing the following command into the command line:

$ composer

If Composer is properly installed, we'll see all the composer commands with a short description of what they do.

Composer commands

At this stage, we expect to see a composer.phar file in the root directory of the website.

Installing the package

In order to install the packages (the code libraries that we want to download from Packagist to our project), the packages need to be registered to a composer.json file within the root directory of the website.

We'll get back to the composr.json later because we first need to find a package to install in the Packagist repository, which is found at packagist.org.

In the packagist.org website, type the name of the package that you want to download in the search box. For the sake of this tutorial, we’ll search for the “faker” package which generates fake data for our projects (something that can be most beneficial during the development stage of a website).

You will be shown a number of packages from which you need to choose the package that suits your needs the best.

Search for a PHP package in the packagist.org website

Click on the package to open the package page. Here you can find relevant information about the package, including the latest version and dependencies. You need to copy the command to download the latest stable version (see the picture below).

Example of a page which is devoted to a PHP package in packagist.org

Back to the terminal, you need to navigate to the root directory of your app or website, and run the command that you just copied from packagist.

$ composer require fzaninotto/faker

Once you press enter, Composer will start to auto-magically download the latest stable version of the package that you require. It also downloads all the packages on which the package depends and generates an autoload file.

At the end of the download process, when you navigate to the root directory, you will see a brand new vendor directory that holds within it all the packages that Composer installed in your website.

The root directory look after package installation with composer

Also in the root directory, you need to create an index.php file.

In the composer.json file you will find the data that composer needs in order to work with, and manage the package that you have just installed. In our example:

{
  "require": {
    "fzaninotto/faker": "^1.5"
  }
}

When you open the vendor folder, you’ll find the autoloader file (autoload.php) that comes free of charge with the download. We require the autoloader in our scripts in order for the Composer libraries to work.

require_once __DIR__ . '/vendor/autoload.php';

To activate the autoloader, you may need to type the following command into the command line:

$ composer dumpautoload -o

Let's write in the index.php file a code that tests if the package that we have just downloaded really works:

<?php
 
require_once __DIR__ . '/vendor/autoload.php';
 
// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();
  
// generate data by accessing properties
echo $faker->name;
  
// generate fake address
echo $faker->address;
  
// generate fake name
echo $faker->text;

How to add packages?

After we have installed our first package, we may very well want to add a second one. This task, can also be done with much ease. All we need to do, is to require the package, according to its name in packagist, and the package will automatically be downloaded to our website. For example, let's require the monolog/monolog package.

$ composer require monolog/monolog

This will download the new package, with all its dependencies to the vendor directory, as well as update the composer.json file. Like so:

{
  "require": {
    "fzaninotto/faker": "1.4.0",
    "monolog/monolog": "~1.13"
  }
}

How to update the packages?

It is possible that, over time, we will have to update the packages and, here again; Composer will come to the rescue! All we need to do in order to update all the packages in your project is to write the following command in the command line:

$ composer update

As a result of this command, Composer will automatically download all the updated versions of the packages, including all the dependencies, straight to the project folder.

How to remove a package from Composer?

In order to remove one of the packages, we need to delete the line corresponding to the package from the composer.json file and update compser:

$ composer update

This will remove the package with all of it dependencies from the vendor directory.

Conclusion

I am sure that the combination of Packagist as a repository of PHP libraries and Composer as a dependency manager can help any PHP project, as it has so faithfully been doing for several years now.

In the next tutorial, we will demonstrate how to Composer autoload our own classes or any other non-packagist class.

comments powered by Disqus