Created Friday 31 October 2014
- Project with dependencies is defined with a JSON file "composer.json"
- Composer reads the contents and connects to Packagist to resolve the dependencies recursively
- The dependencies are downloaded to a local dir (vendor/ in the case of Laravel)
- The state of the dependencies is saved to a file "composer.lock"
- Composer generates an "autoload.php" file at the root of the "vendor/" dir
- If this file is included in a PHP script it wires up the auto-loading of classes
- "require 'vendor/autoload.php"
- **curl -sS https://getcomposer.org/installer | php**
- Make global
- sudo mv composer.phar /usr/local/bin/composer
- Check installation
Creating a new Laravel app
- Navigate to its root dir and run
- composer create-project laravel/laravel --prefer-dist
- Dir "laravel" will be created
Installing a package
- Search https://packagist.org for "image laravel"
- May use a dev-master branch in development, but in production stick with a precise version number (avoid compatibility issues)
- Open composer.json and insert package name and the desired version in the require object:
- Run "composer validate" to validate JSON file
- composer install
- composer update
- To update the composer.lock file and save the exact version numbers of the resolved dependencies
- This command always checks the latetst version of every package, thus it is risky to run it in production (compatibility issues)
- Commit the composer.lock file to VCS repository to make sure that everyone in collaboration works with the exact same versions of the dependencies
- Don't add the contents of the vendor/ dir in VCS (it's already excluded in .gitignore file)
- As long as there is a composer.json and composer.lock files, anyone will be able to resolve the dependencies
- Don't edit any files inside vendor/, since these will be overwritten the next run of composer install
- This option is for stable releases and avoiding downloading the entire Git history if possible
- The complete Git history of every package is included
- When deploying a Laravel app, run composer install on the server
- For FTP deployment there are packages such as barryvdh/laravel-vendor-cleanup that may be used to remove the non-essential files before uploading everything to the server
- Composer's diagnose command and the verbosity flags (-v|vv|vvv) can help identify common problems and useful for help requests on forums, Stack Overflow, etc..