To manage dependencies with Composer, one must understand the fundamental concepts and commands of using Composer as a dependency manager for PHP. Composer allows you to declare the libraries and dependencies your project requires, and manages (install/update) them for you.
First, you need to install Composer. You can do this by downloading it using the following command:
```
php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);“
php composer-setup.php
php -r “unlink(‘composer-setup.php’);“
```
After installation, you should find the `composer.phar` file in your directory. You might want to move it to a directory that’s in your path or create a global installation.
```
mv composer.phar /usr/local/bin/composer
```
To create a new Composer project, you need a `composer.json` file. You can create this manually or use Composer’s command to initialize it:
```
composer init
```
This command walks you through creating the `composer.json` file interactively, where you can specify the project’s name, type, dependencies, and other metadata.
To add a dependency to your project, use the `require` command. For example, to add Guzzle, a PHP HTTP client:
```
composer require guzzlehttp/guzzle
```
This command updates your `composer.json` and adds the package to your `vendor` directory, where all Composer-managed packages reside. Your `composer.json` now includes:
```
{
“require”: {
“guzzlehttp/guzzle”: “^7.0“
}
}
```
To update your dependencies, you can run:
```
composer update
```
This checks your `composer.json` file and updates all dependencies. If you wish to update a specific package:
```
composer update guzzlehttp/guzzle
```
If you no longer need a package, you can remove it with:
```
composer remove guzzlehttp/guzzle
```
This removes the package from your `vendor` directory and your `composer.json`.
Composer provides an autoload feature, which allows you to automatically load dependencies. Include the following in your code:
```
require ‘vendor/autoload.php’;
```
Composer creates a `composer.lock` file upon running `composer install` or `composer update`. This file locks the specific versions of dependencies the project is using, ensuring consistency across environments. Commit this file to version control.
```
composer install
```
The `install` command reads from the `composer.lock` to install the exact versions.
1. Laravel: Laravel uses Composer extensively to manage its internal and third-party packages. Upon creating a new Laravel project, the `composer.json` file comes pre-configured with Laravel’s dependencies, and running `composer install` sets up the project environment.
1. Symfony: Similar to Laravel, Symfony leverages Composer for managing dependencies. Symfony includes additional utilities in its `composer.json`, like scripts for common tasks such as setting up databases or running tests.
1. Composer Documentation: This is an official and comprehensive resource that covers all aspects of managing dependencies with Composer. (https://getcomposer.org/doc/)
2. Packagist: The default Composer package repository where you can find thousands of PHP libraries. (https://packagist.org/)
3. Laravel Documentation: For framework-specific Composer usage examples. (https://laravel.com/docs)
4. Symfony Documentation: For Symfony-specific Composer usage. (https://symfony.com/doc/current/setup.html)
In summary, Composer streamlines the process of managing PHP dependencies, ensuring you have a consistent environment and easily managing library updates and configurations. By using commands like `require`, `update`, `install`, and features like autoloading and lock files, developers can effectively manage dependencies in their projects.