Lesson Monday

Lesson goals:

  • Learn about managing dependencies with Composer

In this lesson we're going to use Composer, which we downloaded in a previous lesson. It is responsible for adding dependencies to your projects. Dependencies are pieces of code that your project is depending on to do its job. These could be PHP libraries written by other people, or if we use the Bootstrap CSS files, that would be a dependency.

Using Composer in a project

To use Composer with a project, create a file called composer.json in your project folder. Inside of the file we will add some text to tell Composer which dependencies we need. For example, in the next lesson we're going to start using a library called Silex. To use it in a project our composer.json file will look like this:

composer.json
{
    "require": {
        "silex/silex": "~1.1"
    }
}

To add another dependency, we add it to the composer.json file. For example, let's add PHPUnit - another framework we will be using shortly. We would make the composer.json file look like this:

composer.json
{
    "require": {
        "silex/silex": "~1.1",
        "phpunit/phpunit": "4.5.*"
    }
}

This means that our app requires both Silex and PHPUnit. The numbers next to the name of each dependency tell composer which version to get. Don't forget to put both the dependency name and the version number in quotes and separate dependencies by commas.

When we are finished making the composer.json file, we can go into the Terminal, change directory into our project folder (in the same place as the composer.json file), and then tell Composer to download our requirements by running this command:

> composer install

Composer will create a new folder called vendor inside of your project folder and place all the files that you need inside. It will also create a file called composer.lock, which you should commit with your other files but you never need to edit it manually. It contains information about the exact versions of each dependency you have installed.

The documentation for most dependencies will tell you exactly what name and version number to put in the composer.json file.

At Epicodus, you'll need to run:

> composer install --prefer-source --no-interaction

If you're curious why, this is because Composer uses the Github API to fetch dependencies by default. The API has a rate limit that prevents one IP address from making too many requests at the same time and overwhelming the API, but it has the effect of blocking us when a bunch of students try to run composer install at the same time. The extra options above tell Composer to use Git instead, which doesn't have a rate limit.

Dependencies are pieces of code that your code relies on. Usually they are libraries or frameworks written by other people that you can include in your project to give it new functionality.

Here is a basic composer.json file with 2 dependencies in it:

composer.json
{
    "require": {
        "silex/silex": "~1.1",
        "phpunit/phpunit": "4.5.*"
    }
}

After creating the composer.json file in the top level of your project folder, go to the terminal, change directory into the same folder as your composer.json file and then run this command:

> composer install

At Epicodus, run:

> composer install --prefer-source --no-interaction