Composer 101

Tutorial: How to use Composer

Composer is a dependency manager for PHP, written in PHP. Specifically, it’s used to simplify the process of using PHP libraries in your projects. The use can range from getting a framework, including a library class, or open source projects; generally these packages are downloaded by Composer and then implemented by a developer in a website’s code.

Examples being: Silex MicroFramework, the infamous PHPMailer class, Laravel Framework, and many more – all of which can be found on Composer’s main repository Packagist.

To find a more documented list of classes, libraries, or frameworks that are available you can review the Packagist site.

So How Do I Use This Tool?

To use Composer you will want to be logged in via SSH, or TTY, and you will run the following command:

$ composer require {somepackage}

This will issue a command for Composer that essentially states you are requiring the provided package name in the current folder. What this does is requests the package given; if found, it then creates the following:

  • composer.json
  • /vendor
  • composer.lock

Ok, so that downloaded the necessary PHP files to use the library, or package, you are requesting. It created a “/vendor” folder where it downloaded all the ‘library’ files. Great, you’ve got the necessary files, but now what?

How Do I Use the Composer-provided Libraries?

To use Composer-provided libraries you may need to understand Composer a little bit better first; specifically you’ll need to understand how Composer’s autoloader works. Think of this as your ‘crash course’ to the autoloader.

To make more sense of this concept let’s go back to our ‘What is Composer’ section; as we know Composer is a “Dependency Manager for PHP”. This means it’s getting you a library, or some code, needed for your site or project. It will also grab any libraries, or other requirements, needed by that library to function – hence the name “dependency management tool”, it’s doing that part in the background for you. In order to make use of this tool, you need to make sure you’re properly including everything that is required.

That’s where Composer’s Autoloader comes in, the autoloader is a script which includes and loads any needed dependencies. Long story short, Composer will create for you a file called: “autoload.php” that is located in the “/vendor” folder it creates. This file is all you need to use/re-use the libraries, classes, or etc, that you are getting from Composer.

Using the Autoloader

In order to use a Composer-provided library you will simply need to add the following to your script:
require 'vendor/autoload.php';

Keep in mind that the part within single quotes should be the relative path to this file; this may need adjusting depending on how you are doing things.

Final Tips When Using Composer

Ultimately, including the `require autoloader.php` line allows you to utilize the packages, classes, libraries, etc, that you are asking composer to get for you. These dependencies are downloaded locally to where the ‘require’ command was run.

When working on cPanel servers you should only run and use composer as the cPanel user requiring those dependencies. Doing this will ensure the files are downloaded in a manner that keeps proper user ownership and permissions. However, if you accidentally run Composer as the root user you will simply need to adjust the file permissions and ownership accordingly.

While the usage of Composer is mainly done by developers, if you are experiencing any issues with file ownership, permissions, or if you have any questions feel free to contact our Heroic Support®.

Be Sociable, Share!

About the Author: Dan Pock

Dan Pock does Technical Writing & Marketing at Liquid Web with a background in System Administration, Public Relations, and Customer Service.

His favorite things include: his cats, Oscar Boots and Dash Nouget; experimenting with PHP; and making up recipes (or at least attempting to). You can find his coding hijinks on GitHub, where he shares most of his projects and open source work.

Here's 75 % off, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support