#Composer is a dependency management tool for PHP. If you are a PHP developer, then you probably use it every day and run the commands require, install and update frequently. In case you didn't realize it, we can use some other Composer commands to make it easier to use.
There are many commands you can use, but today I will share 6 commands that I use and that help me every day.
show
Let’s talk about the show command first. It allows you to view all installed packages for your project (including dependencies), as well as view descriptions of related packages. All this information can be found in the Composer lock file (composer.lock), but using the show command is a relatively simpler and more convenient way to view package information.
To list all installed packages with their version numbers and descriptions, just use show:
composer show
Sometimes put these It will be easier to understand if the information is viewed in the form of a dependency tree. You can pass the --tree or -t parameter:
composer show -t
If you want to filter the returned package, you can pass an additional string argument using wildcards *:
composer show 'symfony/*'
This will return all installed symfony packages. Pay attention to the quotation marks here. If you are using the bash shell, you do not need to add these quotation marks, but if you are using zsh and you do not need the quotation marks, a ‘no matches found’ error will be reported.
If you want to see information about a specific package, you need the full package name:
composer show laravel/framework
This will show you the installed version, its license and dependencies, and where it is installed locally.
why
If you want to know why a specific package is installed, you can use the why command to determine which dependencies require it:
composer why vlucas/phpdotenv
why why is an alias for the depends command, but personally, I find it easier to remember using 'why'. You can view this information in the dependency tree using the --tree or -t flag:
composer why vlucas/phpdotenv -t
why-not
Sometimes one or more installed packages will prevent the package from being installed or updated. To check which packages are installed, we can use the why-not command (alias prohibits). For example, Laravel 5.8 version of the framework; we can use the why-not command to check for any packages that prevent us from updating the laravel/framework package:
composer why-not laravel/framework 5.8
Again, we can view this information in the dependency tree using the --tree or -t flag:
composer why-not laravel/framework 5.8 -t
outdated
Before using the composer update command, you may want to check the installed packages and see which ones can be upgraded. This can be done using the outdated command.
composer outdated
This command is one of the aliases of composer show -lo.
Return coloring codes to indicate the status of each package according to the semantic version:
Green: 当前安装包已是最新版本 Yellow: 有可升级的更新, 但可能有不兼容的修改。 Red: 有可用的小版本升级 (一般是bug修复)
If you want to highlight small upgraded versions, you can use the outdated command with -- minor-only or -m parameter:
composer outdated -m
status
I find that I often use the parameters of the install and update commands - -prefer-source to handle dependencies installed from source. Then, if I modify any of these dependencies, I need a way to quickly check which packages have been modified. The status command provides a convenient method.
You can use the --verbose or -v parameter to view locally modified packages and files:
composer status -v
I found Using the verbose tag is the most useful way to use this command.
License
Finally, it is very useful to know the license of each package you install. Composer has a convenient licenses command for querying the complete list of licenses:
composer licenses
For more composer-related technical articles, please visit composertutorialcolumn!