Get started with 33% off your first certification using code: 33OFFNEW

A Guide to Lesser-Known Composer Commands for PHP

3 min read
Published on 12th April 2023

Blog Image

Composer is a widely used dependency manager for PHP, simplifying the process of managing libraries and packages in your projects. While most developers are familiar with basic commands like install, update, and require, Composer has many lesser-known commands that can be quite useful. In this guide, we'll explore some of these lesser-known Composer commands to help you get the most out of this powerful tool.

If you are familiar with composer then you may be interested in our PHP Fundamentals Certification, which allows you to flex your knowledge in PHP and the surrounding ecosystem.

1. composer outdated

The outdated command lists installed packages that have newer versions available. This is a useful command to periodically check whether your project's dependencies are up-to-date. To run this command, use:

composer outdated

2. composer depends

The depends command shows you which packages depend on a specific package. This is helpful when you want to remove or update a package, and you need to know if other packages in your project rely on it. To use this command, run:

composer depends <package-name>

Replace <package-name> with the name of the package you're interested in.

3. composer why

The why command is an alias for depends. It serves the same purpose as the depends command and can be used interchangeably:

composer why <package-name>

4. composer licenses

The licenses command displays the licenses of all installed packages in your project. This is helpful for ensuring license compliance, especially in open-source projects. To use this command, run:

composer licenses

5. composer global

The global command allows you to run any Composer command in the global context, meaning it will apply to all projects on your system. This is useful for installing packages that you want to be available globally, such as PHP_CodeSniffer, PHP-CS-Fixer, or PHPStan. To use this command, run:

composer global <command>

Replace <command> with the desired Composer command, like require, install, or update.

6. composer create-project

The create-project command is used to create a new project based on an existing package or repository. This is useful when you want to start a new project using a boilerplate or a specific framework. To use this command, run:

composer create-project <package-name> <destination> [--stability=<stability>]

Replace <package-name> with the name of the package or repository, <destination> with the directory where the project should be created, and <stability> with the desired stability level (e.g., dev, alpha, beta, RC, or stable).

7. composer dump-autoload

The dump-autoload command regenerates the autoload files, optimising the autoloader for performance. This can be helpful after manually adding or removing classes, or when working with a package that doesn't properly maintain its autoload configuration. To use this command, run:

composer dump-autoload

You can also use the --optimize flag to further improve autoloading performance:

composer dump-autoload --optimize

Many frameworks use the dump-autoload command as part of their own optimisation techniques, and it is often run as part of a deployment.

Summary

Composer is a powerful tool for managing PHP dependencies, and while its basic commands are essential for everyday use, it also offers many lesser-known commands that can help streamline your workflow and improve your projects. By exploring and utilising these commands, you can take full advantage of Composer's capabilities and enhance your PHP development experience.