In this article, we will tackle the basics of Composer, and what makes it such a powerful and useful tool.
Before we go into detail, there are two things that we need to have in mind:
-
What Composer is:
As we can see on their website: "Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you."
- What Composer is not:
Composer is not a package manager. It deals with packages but in a "per project" way. While it provides a global installation option, it does not install anything globally by default.
Essentially, Composer allows you to declare and manage every dependency of your PHP projects.
Let's now install Composer so we can see it in action.
Installation
There are two ways of installing Composer: locally and globally. Because Composer is such a useful and widespread tool, I always recommend installing it globally, as you'll probably be working on more than one project at once.
Note that if you use an environment like Homestead Improved, it's installed globally by default. If you're unfamiliar with Vagrant and Homestead, this post will clear things up, and if you'd like to dive deeper, this book will reveal some wild mysteries to you.
To install Composer, follow the instructions for your operating system.
If you're on a Unix system, after installing it you can run the command:
mv composer.phar /usr/local/bin/composer
This moves the composer.phar
file to a directory that's on your path (the location of your filesystem where the OS looks for executable files by default). This way, you can use just the composer
command instead of composer.phar
.
Running the composer
command shows the information page:
Some of Composer's more useful commands are:
composer help <command>
- will show the help page for a given command.composer self update
- Updates composer to the latest available version.composer search
- Searches for packages.composer status
- Shows a list of modified packages.composer diagnose
- Diagnoses the system for common errors.composer status
- Shows a list of dependencies that have been modified locally. If we install something from the source (using the--prefer-source
option), we will end up with a clone of that package in the/vendor
folder. If we make some changes to that package, thecomposer status
command will show us a git status for those changes.composer diagnose
- Diagnoses the system for common errors. This is especially useful for debugging, as it checks for things like connectivity to Packagist, free disk space and git settings.
We will focus our attention on composer init
, composer create-project
and composer require
.
Using Composer
To manage dependencies in a project, Composer uses a json
file. This file describes all the dependencies, and holds some metadata as well.
In this example we will create a new PHP project in which we will need PHPUnit for our unit testing. There are several ways to use Composer to manage this dependency.
Continue reading %Re-Introducing Composer – the Cornerstone of Modern PHP Apps%