w3resource
Composer tutorial

How to use composer installed packages in our project

Introduction

In the previous tutorial, we learned about how to use composer to install packages in our project, we also looked at how to update the already installed packages and set them up properly.

In this tutorial, we will look at how to use import these installed packages into our PHP files and consume the classes in these packages. We will assume that you have composer already installed in your system, but if you don’t have it installed, check our tutorial on  composer installation and follow along.

For the sake of clarity, we will setup a new project, install composer into this project, download a library/package and then use this downloaded library.

To start, we will create a new project directory “Composertuts” and  navigate to this directory from the command line.

We will take a slightly different approach form the one in the previous tutorial to install composer in this directory. In your terminal, run the “composer init” command.

composer init

As you can see from the above code snippet, this command will guide us towards the generation of our “composer.json” file, it will also guide us to setup of vendor and package names correctly.

A complete setup of the composer.json should look like this, with the “require” key, already registered as shown in the code snippet below:

{
    "name": "vitalis/vitalis",
    "description": "A test package to test my composer skills",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "Ogbonna Vitalis",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "require": {
        "monolog/monolog": "1.0.*"
    }
}

In the above code snippet, the Composer generated the composer.json file using the configurations we gave when we ran the “composer init” command. We included the “require” key, telling the composer on the exert packages to install, “monolog” in this case.

The next step is to install the monolog package library by typing using the popular composer command “composer install” on the terminal.

On successful installation, the package (monolog) will be saved in the “vendor” directory with the “composer.lock” file also generated where composer saves the exact version of the monolog package installed.

#USING THE MONOLOG PACKAGE IN OUR PROJECT

#Autoloading: This is a key specified in the composer composer.json file, this key basically tells the composer how and where to locate the library, should it be required in another project.

For libraries that specified autoload information, Composer also generates a vendor/autoload.php file automatically upon installation, where the entry paths into the library are stated as shown in the code snippets below:

<?php

// autoload.php @generated by Composer

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit8694f1a4308bbcb7296d9b064c8bc3f6::getLoader();

To start making use of the different classes in the monolog package we just installed, we will create an “index.php” file in the root directory of our project, then include this generated “vendor/autoload.php” file into this file, then call the classes we want to make use of, as shown in the code snippet below.

require __DIR__ . '/vendor/autoload.php';
$log = new Monolog\Logger('name');
$log->pushHandler(new Monolog\Handler\StreamHandler('app.log', Monolog\Logger::WARNING));
$log->addWarning('Foo');

In the above code snippet, we required the complete path to the “vendor/autoload.php” file generated by the composer, which by default gives us access to all the classes in the installed monolog package.

The other lines of codes mainly instantiated and used the Logger Class of the Monolog package.

Yes! today we have successfully implemented composer in our project, used composer to download a package and imported and  the downloaded package in our “index.php” file.

Finally, in the “composer.json” file we can also specify our own autoload information to guide the composer when our project is being used as a package in another project, but that will be covered in the next tutorial, stay tuned!

Don’t forget to like and share this tutorial with your friends, follow us on social media for more updates.

Previous: A gentle introduction to composer as a dependency manager
Next: Making libraries or packages installable using composer