Composer - PHP Dependency Manager

Composer is a dependency management tool for PHP. Php Composer

Install Composer on Mac OS/Linux/Unix

  1. Visit https://getcomposer.org
  2. Click Download
  3. Run the commands found in your terminal
php composer-setup.php --install-dir=bin --filename=composer

Global Composer

Use this command to make composer available globally. Be sure this directory is in your environment PATH.

mv composer.phar /usr/local/bin/composer

Install Composer on Windows

The easiest way to install Composer on Windows is to download the Windows Composer Setup executable. Your PHP installation should have Open SSL enabled for this to work properly.

Getting Started - composer.json

Create a file called composer.json and place it in the root directory of your project. This is a JSON file and it will list all the dependencies for your PHP project. The following is a sample composer.json file and it only requires one package version 1.0.* called monolog from the monolog vendor.

{
"require": {
"monolog/monolog": "1.0.*"
}
}

Packagist - Main Composer Repository

Packagist is the best place to find Composer packages. Visit Packagist at https://packagist.org/ to search for any Composer package for your PHP web or CLI application.

Autoloading with Composer

Composer will generate an autoload file called autoload.php for you and place it in the vendor/ directory. Add this line to your PHP application to automatically require packages from your composer.json file.

require __DIR__ . '/vendor/autoload.php';

Load your code with composer.json

Add a field called autoload to you composer.json file to register your directory.

{
"autoload": {
"psr-4": {"Webcarpenter": "src/"}
}
}

Adding this to your composer.json will register a PSR-4 autoloader for the Webcarpenter namespace. Files in the directory named src/ will be mapped to Webcarpenter. An example filename would be src/Image.php containing a Webcarpenter/Image class.

Regenerate autoload

If you modify your composer.json file, then you should run these commands to regenerate your vendor/autoload.php

composer dump-autoload

Updating composer.json

To get the latest versions of the dependencies and to update the composer.lock file, you should use the update command.

composer update

 

 

Visit sunny St. George, Utah, USA