Composer Cheat Sheet

Composer is a dependency manager for PHP, widely used in modern PHP projects. Here’s a cheat sheet with some common Composer commands:

Initializing a Project

Initialize a new project:

composer init

Install dependencies from the composer.json file:

composer install

Install a specific package:

composer require vendor/package-name

Managing Dependencies

Update dependencies to the latest version:

composer update

Update a specific package:

composer update vendor/package-name

Remove a package:

composer remove vendor/package-name

Autoloading

Dump autoloader:

composer dump-autoload

Scripts

Run a script from composer.json:

composer run-script script-name

Versioning

Check outdated dependencies:

composer outdated

Show the installed version of a package:

composer show vendor/package-name

Creating a Package

Create a new package skeleton:

composer create-project vendor/package destination

Composer Configuration

Show Composer configuration:

composer config

Set configuration options:

composer config option-name option-value

Miscellaneous

Validate composer.json file:

composer validate

Create a Phar (PHP Archive) of your project:

composer archive

Optimize class loading for production:

composer dump-autoload --optimize --classmap-authoritative

Show help and list available commands:

composer --help

Remember to replace placeholders like vendor/package-name, script-name, and others with the actual values relevant to your use case.

This cheat sheet covers some of the most commonly used Composer commands. For more detailed information, refer to the official Composer documentation.