Catalog / Composer Cheatsheet
Composer Cheatsheet
A comprehensive cheat sheet for Composer, the dependency manager for PHP. Covers installation, dependency management, autoloading, and common commands with examples.
Installation & Basic Usage
Installation
Local Installation: Download Global Installation:
|
Verify Installation:
|
Ensure that PHP is installed and accessible from your command line. |
Basic Commands
|
Initializes a new composer project in the current directory. |
|
Installs the project’s dependencies from the |
|
Updates the project’s dependencies to the latest versions specified in |
|
Adds a new dependency to the |
|
Removes a dependency from the |
|
Regenerates the autoloader files. |
composer.json Structure
|
Dependency Management
Specifying Versions
|
Specific version. |
|
Minimum version, allows later versions. |
|
Maximum version, allows earlier versions. |
|
Equivalent to |
|
Equivalent to |
|
Any version. Not recommended for production. |
|
Install the latest code from the |
Updating Dependencies
|
Always commit your |
If you want to update only single package use |
Resolving Conflicts
If Composer encounters conflicts, it will provide error messages suggesting how to resolve them. This often involves relaxing version constraints in your |
Use |
Consider using the |
Autoloading
PSR-4 Autoloading
PSR-4 is the recommended autoloading standard. Specify the namespace to directory mapping in the
This maps the |
After modifying the |
Classmap Autoloading
Classmap autoloading scans specified directories for PHP classes and builds a map.
Run |
Files Autoloading
Files autoloading includes specified PHP files.
Run |
Optimizing Autoloading
Use the
This generates a single |
Advanced Usage
Scripts
Define custom scripts in the
Run scripts using
|
Repositories
Configure custom repositories in the
|
Supported repository types include |
Platform Configuration
Specify platform requirements in the
|
This is useful for ensuring compatibility on different environments. |
Plugins
Composer plugins extend Composer’s functionality. Install plugins like any other dependency. Ensure
|