php依赖管理工具Composer 安装及使用(windows):

677 阅读5分钟

1、安装:

下载地址:getcomposer.org/download/

选择php路径

如果报下面的错,切换php版本:

安装成功(报错重启电脑):

如果下载依赖包的速度慢可以切换至中国镜像(-g 全局):

 composer config -g repo.packagist composer https://packagist.phpcomposer.com

解除中国镜像(-g 全局):

composer config -g --unset repos.packagist

2、常用命令的使用:

新建composer.json文件:

reuqire命令(添加所需的依赖包):

 composer require phpunit/phpunit:~4.8

效果:

  • 最新版: *
  • 大于等于1.0的最高版本:>=1.0
  • 大于等于1.0且小于2.0的最高版本: >=1.0 <2.0
  • 1.0至2.0之间的最高版本:1.0 - 2.0
  • 大于等于1.0且<1.1的最高版本: 1.0.*
  • 大于等于1.2且<2.0的最高版本:~1.2
  • 大于等于1.2.0且<1.3.0最高版本:~1.2.0
  • 大于等于1.2.0且<2.0.0的最高版本:^1.2.0

update命令(更新依赖包版本):

 composer update phpunit/phpunit

remove命令(移除所需的依赖包)

 composer remove phpunit/phpunit

search命令:

composer search phpuni

效果(模糊搜索):

composer search phpuni  --only-name phpuni

效果(精确搜索):

show命令(展示项目中的已安装依赖的信息):

composer show 

list命令(显示所有composer相关命令):

composer list

3、composer命令和选项列表:

Usage(用法):
  command [options] [argument] (命令 [选项] [参数])

Options(选项):
  -h, --help                     Display this help message(显示此帮助消息)
  -q, --quiet                    Do not output any message(不输出任何消息)
  -V, --version                  Display this application version(显示此应用程序版本)
      --ansi                     Force ANSI output(强制ANSI输出)
      --no-ansi                  Disable ANSI output(禁用ANSI输出)
  -n, --no-interaction           Do not ask any interactive question(不要问任何互动问题)
      --profile                  Display timing and memory usage information(显示时间和内存使用信息)
      --no-plugins               Whether to disable plugins.(是否禁用插件)
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.(如果指定,则使用给定目录作为工作目录。)
      --no-cache                 Prevent use of the cache(e禁止使用缓存)
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug (增加消息的详细程度:1表示正常输出,2表示更多详细输出,3表示调试)

Available commands(可用命令):
  about                Shows the short information about Composer.(关于显示有关Composer的简短信息。)
  archive              Creates an archive of this composer package. (存档创建此Composer程序包的存档)
  browse               Opens the package's repository URL or homepage in your browser.(浏览在浏览器中打开软件包的存储库URL或主)
  cc                   Clears composer's internal package cache.(清除Composer的内部程序包缓存。)
  check-platform-reqs  Check that platform requirements are satisfied.(检查是否满足平台要求)
  clear-cache          Clears composer's internal package cache.(清除Composer的内部程序包缓存)
  clearcache           Clears composer's internal package cache.(清除Composer作曲家的内部程序包缓存)
  config               Sets config options.(设置配置选项)
  create-project       Creates new project from a package into given directory.(从包中创建新项目到给定目录)
  depends              Shows which packages cause the given package to be installed.(显示哪个软件包导致给定软件包的安装)
  diagnose             Diagnoses the system to identify common errors.(诊断系统以识别常见错误)
  dump-autoload        Dumps the autoloader.(转储自动加载器)
  dumpautoload         Dumps the autoloader.(转储自动加载器)
  exec                 Executes a vendored binary/script.(执行供应的二进制文件/脚本)
  fund                 Discover how to help fund the maintenance of your dependencies.(了解如何资助您的依赖项的维护。)
  global               Allows running commands in the global composer dir ($COMPOSER_HOME).(允许在全局composer目录($ COMPOSER_HOME)中运行命令。)
  help                 Displays help for a command(显示命令帮助)
  home                 Opens the package's repository URL or homepage in your browser.(e在浏览器中打开软件包的存储库URL或主页。)
  i                    Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.(从composer.lock文件中安装项目依赖项(如果存在),或者依赖composer.json。)
  info                 Shows information about packages.(显示有关软件包的信息。)
  init                 Creates a basic composer.json file in current directory.(在当前目录中创建一个基本的composer.json文件。)
  install              Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.(从composer.lock文件中安装项目依赖项(如果存在),或依靠composer.json。)
  licenses             Shows information about licenses of dependencies.(许可证显示有关依赖项许可证的信息。)
  list                 Lists commands(列出命令)
  outdated             Shows a list of installed packages that have updates available, including their latest version.(已过时显示具有可用更新(包括其最新版本)的已安装软件包的列表。)
  prohibits            Shows which packages prevent the given package from being installed.(禁止显示哪些软件包阻止安装给定的软件包。)
  remove               Removes a package from the require or require-dev.(从require或require-dev中删除一个软件包。)
  require              Adds required packages to your composer.json and installs them.(将必需的软件包添加到您的composer.json并安装它们。)
  run                  Runs the scripts defined in composer.json.(运行composer.json中定义的脚本。)
  run-script           Runs the scripts defined in composer.json.(脚运行composer.json中定义的脚本。)
  search               Searches for packages.(搜索搜索软件包)
  self-update          Updates composer.phar to the latest version.(将composer.phar更新到最新版本)
  selfupdate           Updates composer.phar to the latest version.(将composer.phar更新到最新版本)
  show                 Shows information about packages.(显示有关软件包的信息。)
  status               Shows a list of locally modified packages, for packages installed from source.(显示从源安装的软件包的本地修改软件包的列表。)
  suggests             Shows package suggestions.(显示安装包建议)
  u                    Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.(根据composer.json将依赖项升级到最新版本,并更新composer.lock文件。)
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.(根据composer.json将依赖项升级到最新版本,并更新composer.lock文件。)
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.(根据composer.json将依赖项升级到最新版本,并更新composer.lock文件。)
  validate             Validates a composer.json and composer.lock.(验证composer.json和composer.lock。)
  why                  Shows which packages cause the given package to be installed.(显示哪个软件包导致给定软件包的安装)
  why-not              Shows which packages prevent the given package from being installed.(显示哪个软件包阻止安装给定的软件包)