Home >Development Tools >composer >You must use these Composer commands!

You must use these Composer commands!

藏色散人
藏色散人forward
2020-06-22 13:43:433343browse

The following is the tutorial column of composer to introduce you to the common commands of Composer. I hope it will be helpful to friends in need!

You must use these Composer commands!

selfupdate

Update composer itself, please perform composer selfupdate frequently to keep Composer always the latest version .

<span style="font-size: 16px;">composer selfupdate<br/></span>

is equivalent to

<span style="font-size: 16px;">composer self-update<br/></span>

dumpautoload

when we change the composer.json file When autoloading, you need to execute composer dumpautoload to make the autoload take effect immediately. Without having to execute install or update commands.

<span style="font-size: 16px;">composer dumpautoload<br/></span>

is equivalent to

<span style="font-size: 16px;">composer dump-autoload<br/></span>

The dumpautoload command has two commonly used options:

  • --optimize (-o): Convert PSR-0/4 autoloading to classmap for faster loading. This is particularly suitable for production environments, but may take some time to run, so it is not currently the default.
  • --no-dev: Disable autoload-dev rules.

install

<span style="font-size: 16px;">composer install<br/></span>

is defined based on the composer.lock (lock file) or composer.json file in the current directory. Dependencies, install dependent packages.

The install command will first check whether the composer.lock lock file exists. If it exists, it will download the version specified in the composer.lock file, ignoring the definition in the composer.json file.

<span style="font-size: 16px;"># 查看 composer install 的帮助信息<br/>composer install -h<br/><br/># 只安装 require 中定义的依赖,而不安装 require-dev 中定义的依赖<br/>composer install --no-dev<br/></span>

update

If you want to update your dependency version, or you modify the dependency relationship in composer.json, you want composer To perform update operations as defined in the composer.json file, use the update command.

<span style="font-size: 16px;">composer update<br/></span>

require

require command is generally used to install new dependency packages and write dependencies into the composer.json file in the current directory. .

If dependencies are added or changed in the composer.json file, the modified dependencies will be installed or updated.

<span style="font-size: 16px;">composer require<br/></span>

You can also directly specify the dependency packages that need to be installed in the command.

<span style="font-size: 16px;">composer require barryvdh/laravel-ide-helper<br/></span>

Install the specified version of the dependency package.

<span style="font-size: 16px;">composer require barryvdh/laravel-ide-helper:1.0<br/></span>

--dev option corresponds to require-dev. If your dependency package is only used in the development environment, it is recommended to add the --dev option.

<span style="font-size: 16px;">composer require --dev barryvdh/laravel-ide-helper<br/></span>

create-project

You can use create-project to create a new project from an existing package.

It is equivalent to installing the dependencies of this package into its own vendor directory after executing the git clone command.

This command has several common uses:

  • You can quickly deploy your application.
  • You can check out any resource pack and develop patches for it.
  • For multi-person development projects, you can use it to speed up application initialization.
<span style="font-size: 16px;"># 安装 Laravel 项目<br/>composer create-project --prefer-dist laravel/laravel blog 5.5.*<br/></span>

If no version number is specified, the latest version will be installed by default.

--prefer-dist: Install from dist when a package is available.

The search command can search for remote dependent packages. Usually it only searches for packages on packagist.org. You can simply enter your search terms.

<span style="font-size: 16px;">composer search monolog<br/></span>

--only-name (-N) option, only searches for the specified name (exact match).

show

#To list installed dependency packages, you can use the show command.

<span style="font-size: 16px;">composer show<br/></span>

List all dependent packages on the mirror site.

<span style="font-size: 16px;">composer show --all<br/></span>

If you want to view detailed information about a specified dependent package, you can enter the package name.

<span style="font-size: 16px;">composer show monolog/monolog<br/></span>

config

config 命令允许你编辑 Composer 的一些基本设置,无论是本地的 composer.json 还是全局的 config.json 文件。

查看 Composer 的全局配置信息:

<span style="font-size: 16px;">composer config -g --list<br/></span>

语法:config [options] [setting-key] [setting-value1] ... [setting-valueN]

setting-key 是配置选项的名称,setting-value1 是配置的值。可以使用数组作为配置的值(像 github-protocols),多个 setting-value 是允许的。

例如,全局配置 Composer 的国内镜像:

<span style="font-size: 16px;"># 推荐使用阿里云 Composer 全量镜像<br/>composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/<br/></span>

选项:

  • --global (-g): 操作 $COMPOSER_HOME/config.json 全局配置文件。如果不指定该参数,此命令将影响当前项目的 composer.json 文件,或 --file 参数所指向的文件。
  • --editor (-e): 使用文本编辑器打开 composer.json 文件。默认情况下始终是打开当前项目的文件。当存在 --global 参数时,将会打开全局的 config.json 文件。
  • --unset; 移除由 setting-key 指定的配置选项。
  • --list (-l): 查看当前配置选项的列表。当存在 --global 参数时,将会显示全局配置选项的列表。
  • --file="..." (-f):在一个指定的文件上操作,而不是 composer.json。它不能与 --global 参数一起使用。

run-script

run-script 命令,可用来手动执行脚本,只需要指定脚本的名称即可。

假如,composer.json 中存在如下脚本。

<span style="font-size: 16px;">{<br/>    "scripts": {<br/>        "post-update-cmd": "MyVendor\\MyClass::postUpdate",<br/>        "post-package-install": [<br/>            "MyVendor\\MyClass::postPackageInstall"<br/>        ],<br/>        "post-install-cmd": [<br/>            "MyVendor\\MyClass::warmCache",<br/>            "phpunit -c app/"<br/>        ]<br/>    }<br/>}<br/></span>

运行所有 post-install-cmd 事件下定义的脚本:

<span style="font-size: 16px;">composer run-script post-install-cmd</span><br/>

更多composer技术文章,请访问composer栏目!                  

The above is the detailed content of You must use these Composer commands!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete