Please wait

Composer

Composer is a widely-used dependency management tool in PHP development. It automates the process of handling external libraries and packages, ensuring that the correct versions are installed and that all dependencies are compatible with one another.

Developers can quickly install or update the necessary components with simple commands. Composer also supports autoloading, streamlining the inclusion of external classes in the code. It has become an essential tool in modern PHP development, greatly simplifying the management of dependencies and promoting best practices.

The official site for Composer can be found here: https://getcomposer.org/

Packagist

Before we can use Composer, we should be aware of a critical resource known as Packagist. Packagist is like a big online store where you can find many pre-made pieces of PHP code called packages. These packages are created by other developers and can help you add features to your PHP project without having to write all the code yourself.

Packagist can be found here: https://packagist.org/

When using Composer, the dependency management tool for PHP, Packagist, is the default place where Composer looks for these packages. You can think of Composer as the shopping cart and Packagist as the store.

So, if you want to add a feature like sending emails to your project, you can find a package on Packagist, and Composer will help you download and include it in your code. It makes development quicker and easier!

Installing Composer

Now that we know where to find packages, we can install Composer. The Composer documentation already has great installation instructions, so we're going to advise you to refer to the official documentation for installing Composer.

The official installation instructions can be found here: https://getcomposer.org/doc/00-intro.md

Verifying Composer Installation

After you've installed Composer, you'll want to verify it works. You can do so by running a command called composer about like so:

composer about

It should output something like this:

Composer - Dependency Manager for PHP - version 2.5.8
Composer is a dependency manager tracking local dependencies of your projects and libraries.
See https://getcomposer.org/ for more information.

The composer command becomes available after installation. It has a series of subcommands, which you can view by just running the composer command without anything else:

composer

Configuration File

Composer is meant to be used on a per-project basis. Before you can start installing dependencies, make sure your command line is pointed to your current project. After doing so, you must create a composer.json file.

The composer.json file contains the settings and list of dependencies for your current project. When you want to add a new feature to your project, you might need to include a specific package. You write down the name and version of that package in the composer.json file. Then, when you run Composer, it looks at this file and downloads everything on the list for you.

There are two ways to create this file. You can manually create it, or you can use a command. Typically, using a command to create the file reduces the likelihood of errors. In the command line, you can run the following command:

composer init

After doing so, Composer is going to ask you a series of questions related to your project. Answer these questions however you like. Once you've completed the questionnaire, you'll get a new composer.json file in your project that may look something like this:

{
  "name": "my-project",
  "require": {}
}

There are two properties:

  • name - The name of your project.
  • require - A list of dependencies.

The composer.json file can have various properties. A complete list can be found here: https://getcomposer.org/doc/04-schema.md

Adding a Dependencies

Installing a dependency with Composer is super simple. Let's go through it step-by-step.

  1. Navigate to your project directory. Use the cd command to go to the folder where your project is located.
  2. Find the package on Packagist. Search for the package you need on Packagist (the online store for PHP packages) and note down the name. For this example, we're going to use the Twig package.
  3. Use the install command. Type the following command:
# Format
composer require vendor/package
 
# Real Example:
composer require twig/twig
  1. Replace vendor/package with the actual name you found on Packagist. In our case, the package name would be twig/twig.
  2. Wait for installation! Composer will download the package and add it to your project. It will also update the composer.json file with the new dependency.
  3. Check the composer.json file. Open this file in your project to see the new dependency listed under the require object.

That's it! You've added a new dependency to your PHP project using Composer. It's a straightforward process that ensures you have the right versions of the code you need, all managed and organized for you.

composer require twig/twig

Installing Dependencies

Composer creates a directory called vendor. This directory contains all dependencies downloaded with Composer.

If you plan on sharing your project or downloading a project from somewhere else, it's highly recommended to delete the vendor folder. It's not necessary to share it.

But how would you install dependencies, then? One of the main advantages of the composer.json file is that it contains a complete list of dependencies. You can run the following command to instruct Composer to download all dependencies listed in this file:

composer install

Autoloading Dependencies

Composer sets up autoloading for its dependencies. When you use Composer to install dependencies, it follows certain rules to figure out where the classes and files are located within those packages. These rules are often defined by the package creators.

Composer then creates a file called autoload.php in the vendor directory of your project. This file is like an index or catalog that knows where everything is.

To make sure that PHP knows about this autoloader, you must include it at the beginning of your project. You simply add the following line to the main PHP file of your project:

require 'vendor/autoload.php';

Now, when you use a class from a package in your code, the autoloader automatically finds and includes the right file for you. You can start using any of your dependencies classes right away.

For example, you can start using Twig's classes to set up a template engine like so:

$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, [
  'cache' => '/path/to/compilation_cache',
]);

Updating Dependencies

Updating dependencies with Composer is an important maintenance task to keep your PHP project up-to-date and secure. It can be done for individual packages or all at once, and it's something that should be done regularly, just like checking and refreshing the ingredients in your kitchen. It ensures your project continues to work smoothly with the latest features and security patches.

Before updating, you may want to see what updates are available. You can do this by running:

composer outdated

This will list the packages that have newer versions available. If you want to update a specific package, you can run:

composer update vendor/package

Replace vendor/package with the actual name of the package you want to update. If you want to update Twig specifically, you can run the following command:

composer update twig/twig

To update all the dependencies to the latest versions allowed by the constraints in your composer.json file, run:

composer update

After updating, it's a good practice to run any tests you have in your project to make sure everything still works together.

If your project is under version control (like Git), you'll want to commit the changes to the composer.lock file after updating. This file keeps a record of the exact versions of each package. Committing it ensures that other developers (or even your future self) use the exact same versions. Think of this as writing down your recipe changes so you can recreate the dish later.

Global Dependencies

Dependencies are always installed for a specific project. If you install a dependency in one project, it's not automatically available in other projects. You would have to reinstall the dependency for each project. Sometimes, you may want dependencies to be available globally. Composer supports global dependencies.

Global dependencies in Composer are packages that are installed once on your system and can be used across multiple projects. They are useful for general development tools but should be used with caution to avoid potential conflicts. Installing them is done with the composer global require command, and they can be a handy part of your PHP development toolkit.

To install a package globally, you use the global command with Composer. The command might look like this:

composer global require vendor/package

Replace vendor/package with the actual name of the package you want to install globally.

The globally installed packages are usually stored in a specific directory in your home folder. Once a package is installed globally, you can use it in any of your PHP projects.

Using global dependencies sparingly

Global dependencies are great for tools that you use in many projects, like coding standards checkers or testing tools. But for libraries specific to a project, it's better to include them in that project's composer.json file. If different projects require different versions of a globally installed package, you might run into problems.

Key Takeaways

  • Composer is a dependency management tool for PHP.
  • You can add packages to your project using the composer require command
  • Composer allows updating individual or all dependencies, ensuring your project stays up-to-date.
  • Composer's autoloading feature automatically includes the right files when needed, like having an automatic filing system for your code.
  • Global dependencies are packages usable across multiple projects. Installed with composer global require.
  • The composer.json file is the heart of your project's dependencies, listing what packages and versions are required.
  • Packagist is the main repository for PHP packages,.
  • The composer.lock file keeps a record of the exact versions used, ensuring consistency across environments.

Comments

Please read this before commenting