Unlocking Composer: Useful and Lesser-Known Commands Every PHP Developer Should Know
Composer has become an indispensable tool for PHP developers. While most of us are familiar with commands like composer install
and composer update
, there are several lesser-known commands that can simplify workflows and enhance productivity. Let’s dive into some of these hidden gems.
1. composer outdated
What it does:
Lists all dependencies that have newer versions available. It’s a great way to keep track of outdated packages without immediately updating them.
Usage:
composer outdated
Pro tip: Use --direct
to only show outdated dependencies you explicitly require.
composer outdated --direct
2. composer show
What it does:
Displays detailed information about installed packages, including version, description, and dependencies.
Usage:
composer show
Pro tip: Add the package name to get details about a specific dependency:
composer show vendor/package
3. composer dump-autoload
What it does:
Regenerates the autoloader files. Useful after adding classes or changing namespaces without modifying composer.json
.
Usage:
composer dump-autoload
Pro tip: Use the --optimize
flag for production environments to create an optimized class map.
composer dump-autoload --optimize
4. composer global
What it does:
Allows you to install and manage global Composer packages, such as tools like phpunit
or laravel/installer
.
Usage:
composer global require vendor/package
Pro tip: Check global dependencies with:
composer global show
5. composer depends
(or why
)
What it does:
Shows which packages depend on a specific dependency. Useful for debugging dependency conflicts.
Usage:
composer depends vendor/package
Pro tip: Use composer depends --tree vendor/package
to display the dependency tree.
6. composer prohibits
(or why-not
)
What it does:
Shows which dependencies prevent a specific package or version from being installed.
Usage:
composer prohibits vendor/package
7. composer validate
What it does:
Checks the validity of your composer.json
file and highlights errors or warnings.
Usage:
composer validate
Pro tip: Use --no-check-publish
to skip checking publishability for private packages.
8. composer fund
What it does:
Lists funding information for dependencies, helping you discover projects that could use financial support.
Usage:
composer fund
9. composer create-project
What it does:
Sets up a new project by installing a specific package into a directory.
Usage:
composer create-project vendor/package my-project
Pro tip: Use --prefer-dist
to download a package’s distribution archive for faster installations.
10. composer self-update
What it does:
Updates Composer itself to the latest version.
Usage:
composer self-update
Pro tip: Revert to a specific version using:
composer self-update --rollback
Conclusion
These lesser-known Composer commands can significantly improve your PHP development workflow. By mastering them, you’ll not only save time but also manage dependencies more effectively. If you have any hidden gems to share, feel free to leave a comment below!