Composer - Dependency Management for PHP - helps you declare, manage and install dependencies of PHP projects.
Installation
homebrew
Install brew/update brew
brew update brew tap homebrew/dupes brew tap homebrew/phpInstall the desired php version in terminal
brew install php56Install composer
brew install composer
Commands
how to use composer
Create basic composer.json file in current directory
composer initthis would guide you through creating composer.json
"require": { "monolog/monolog": "1.0.*" }Install the dependencies by running composer’s install command
'composer install —no-dev' # install dependencies from "require" but not from "require-dev" 'composer install' # will install from bothThis would create:
a vendor directory in your project with the required libraries and an auto loader used to load them into the project.
a composer.lock file
a autoload.php file within the /vendor directory
Info
Update command will fetch the latest matching versions (according to your composer.json file) and also update the lock file with the new version
Install command checks if a lock file is present, and if there is, it downloads the versions specified there (regardless of what composer.json says) Otherwise, composer will read the dependencies and versions from composer.json and create a lock file
Require composers auto loader in to your PHP script with
require '/path/to/vendor/autoload.php';Optional - Updating packages
composer update —no-dev # update dependencies from "require" but not from "require-dev" composer update # will update from both
update v install commands
composer update will update dependencies specified in composer.json
composer install will install dependencies specified in composer.lock
composer update will:
- Read composer.json
- Remove installed packages that are no more required in composer.json
- Check the availability of the latest versions of your required packages
- Install the latest versions of your packages
- Update composer.lock to store the installed packages version
composer install:
- Read composer.lock file
- Install the packages specified in the composer.lock file
composer.json schema
https://getcomposer.org/doc/04-schema.md#autoload
‘require’
- Lists packages required by this package
‘require-dev’
- Lists packages required for developing this package, or running tests, etc.
- Both install or update support the
—no-devoption that prevents dev dependencies from being installed.‘repositories’
- By default Composer just uses the packagist repository. By specifying repositories you can get packages from elsewhere.
‘autoload’
- Autoload mapping for a PHP autoloader.
- PSR-4 and PSR-0 autoloading, classmap generation and files includes are supported.
- ‘PSR-4’
- /vendor/composer/autoload_psr4.php
- ‘PSR-0’
- /vendor/composer/autoload_namespaces.php
- ‘classmap’
- /vendor/composer/autoload_classmap.php
- You can use the classmap generation support to define autoloading for all libraries that do not follow PSR-0/4. To configure this you specify all directories or files to search for classes.
- ‘files’
- Use if you want to require certain files explicitly on every request
- This is useful if your package includes PHP functions that are not in a class