Composer is a tool for handling and managing dependencies in PHP. It allows to declare the libraries for the projects and manages by installing or updating that.
It creates composer.json
file for declaring dependencies for the project. Which require libraries with versions. It can download and install the specified dependencies automatically. It handles complex dependency relationships to ensure compatibility between different libraries.
-
Class Autoloading: Composer generates an autoloader that automatically loads classes as needed, reduces usage for manual
require
orinclude
statements. -
PSR-4 Autoloading: Composer can support PSR-4 autoloading standards, which makes project structure and class loading more predictable.
-
Package Discovery: Composer can discover packages from different sources including
Packagist
which is a public repository for PHP packages. -
Package Updates: Composer can easily update dependencies to their latest versions update command.
-
Custom Packages: Composer can be used for creating custom packages of code and share it with others.
- Install Composer: To install composer, follow the instructions from official site to install.
- Initiate Composer: Initiate project with composer by using
composer init
command. - Add Libraries: After initiation, mention third party libraries in
composer.json
or by command. - Download Libraries: After updating
composer.json
, runcomposer install
command to download and install the required dependencies.
Example:
{
"require": {
"php": "^8.1"
}
}
Example: Add the composer autoload file in the PHP script to load dependencies.
require 'vendor/autoload.php';
-
Core Commands:
init
: It create a new project and initialize Composer.composer init
install
: It installs all dependencies listed in thecomposer.json
file.composer install
update
: It updates all dependencies to their latest compatible versions.composer update
require
: Adds a new dependency to thecomposer.json
file.composer require monolog/monolog
remove
: Removes a dependency from thecomposer.json
file and deletes it.composer remove <package-name>
show
: Shows information about packagescomposer show
create-project
: Creates a new project based on a specified package template.composer create-project laravel/laravel my-project
-
Additional Commands and Flags:
self-update
: Updates Composer itself to the latest version available.outdated
: Checks for outdated dependencies and packages.--dev
: It used withrequire
command which adds and installs a development dependency.--no-dev
: It used withrequire
command which adds and installs only production dependency.--no-interaction
: Skips interactive prompts for the commands.--prefer-dist
: Prefers installing packages from distributions.--prefer-source
: Prefers installing packages from source.--optimize-autoloader
: Optimizes the autoloader for better performance.--dry-run
: Simulates a command without making changes.
Example:
Optimizes the autoloader
composer dump-autoload -o
Checks for outdated packages
composer outdated