Using Composer the Right way
Akshay Khale
If you are not a PHP developer and have no idea about composer then you can skip this article.
If you are a PHP developer then you must be using Composer for Dependency management then most of the time (just like almost every programmer) you must be doing composer install or composer update and then composer dumpautoload even on the Production Server (I was doing the same mistake).
By default composer install will install all the packages listed in the composer.json file and it is recommended to skip dev packages on Production Server.
so, here I am listing some useful composer commands which will help you to install only the required packages with proper auto-loading.
- Instead of
composer dumpautoloadusecomposer dumpautoload -oorcomposer du -o:composer dumpautoload -owill take more time thatcomposer dumpautoloadbut after the command, the file autoloading will be faster due to PSR-0/4 autoloading to class-map. - Instead of
composer installusecomposer install --no-dev --optimize-autoloader: Composer by default installs all the packages from composer.json and--no-devwill prevent it from doing so.--optimize-autoloadwill optimize autoloader for after autoloading (as mentioned in point 1) post-installation. composer installrecommendation for build scripts: If you are using a CI system then here is the recommendedcomposer installcommand for build scripts:composer install --no-ansi --no-dev --no-interation --no-progress --no-scripts --optimize autoloader, what is does a. Skip Dev dependencies (--no-dev) b. Optimize autoloader (--optimize-autoload) c. Stop installation scripts (might cause issue during the build but completely dependent on your business logic)(--no-scripts) d. It will not print progress and will work without generating o/p or asking for any i/p (--no-progress--no-interactionand--no-ansi)
You can read more about the composer CLI and all the options provided by the composer over here: https://getcomposer.org/doc/03-cli.md
References: