您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP Composer安装&使用

2024/4/21 15:34:36发布23次查看
基本用法基本用法安装display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json:项目安装关于 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>require key包名称包版本下一个重要版本(波浪号运算符)稳定性安装依赖包display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.lock - 锁文件packagist自动加载安装安装 composer,你只需要下载 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.phar 可执行文件。curl -ss https://getcomposer.org/installer | php详细请查看 简介 章节。要检查 composer 是否正常工作,只需要通过 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>php 来执行 phar:php composer.phar这将返回给你一个可执行的命令列表。注意: 你也可以仅执行 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>--check 选项而无需下载 composer。 要获取更多的信息请使用 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>--help。
curl -ss https://getcomposer.org/installer | php -- --helpdisplay:inline-block; line-height:33.75px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json:项目安装要开始在你的项目中使用 composer,你只需要一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json 文件。该文件包含了项目的依赖和其它的一些元数据。这个 json format 是很容易编写的。它允许你定义嵌套结构。关于 display:inline-block; line-height:27px; margin:0px 2px; background-color:rgb(249,242,244)>require key第一件事情(并且往往只需要做这一件事),你需要在 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json 文件中指定 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>require key 的值。你只需要简单的告诉 composer 你的项目需要依赖哪些包。{ require: { monolog/monolog: 1.0.* }}你可以看到, display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>require 需要一个 包名称 (例如 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>monolog/monolog) 映射到 包版本 (例如 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.*) 的对象。包名称包名称由供应商名称和其项目名称构成。通常容易产生相同的项目名称,而供应商名称的存在则很好的解决了命名冲突的问题。它允许两个不同的人创建同样名为 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>json 的库,而之后它们将被命名为 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>igorw/json 和 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>seldaek/json。这里我们需要引入 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>monolog/monolog,供应商名称与项目的名称相同,对于一个具有唯一名称的项目,我们推荐这么做。它还允许以后在同一个命名空间添加更多的相关项目。如果你维护着一个库,这将使你可以很容易的把它分离成更小的部分。包版本在前面的例子中,我们引入的 monolog 版本指定为 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.*。这表示任何从 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0 开始的开发分支,它将会匹配 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.0、display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.2 或者 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.20。版本约束可以用几个不同的方法来指定。名称实例描述
确切的版本号 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.2 你可以指定包的确切版本。
范围 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.0 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.0,display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.0,=1.2 通过使用比较操作符可以指定有效的版本范围。 
有效的运算符:display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>、display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=、display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>、display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>、display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>!=。 
你可以定义多个范围,用逗号隔开,这将被视为一个逻辑and处理。一个管道符号display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>|将作为逻辑or处理。 
and 的优先级高于 or。
通配符 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.* 你可以使用通配符display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>*来指定一种模式。display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>1.0.*与display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.0,是等效的。
赋值运算符 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2 这对于遵循语义化版本号的项目非常有用。display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2相当于display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.2,。想要了解更多,请阅读下一小节。
下一个重要版本(波浪号运算符)display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~ 最好用例子来解释: display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2 相当于 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.2,,而 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2.3 相当于 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>>=1.2.3,。正如你所看到的这对于遵循 语义化版本号 的项目最有用。一个常见的用法是标记你所依赖的最低版本,像 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2 (允许1.2以上的任何版本,但不包括2.0)。由于理论上直到2.0应该都没有向后兼容性问题,所以效果很好。你还会看到它的另一种用法,使用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>~ 指定最低版本,但允许版本号的最后一位数字上升。注意: 虽然 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>2.0-beta.1 严格地说是早于 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>2.0,但是,根据版本约束条件, 例如 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2 却不会安装这个版本。就像前面所讲的 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>~1.2 只意味着 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>.2 部分可以改变,但是 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>1. 部分是固定的。
稳定性默认情况下只有稳定的发行版才会被考虑在内。如果你也想获得 rc、beta、alpha 或 dev 版本,你可以使用 稳定标志。你可以对所有的包做 最小稳定性 设置,而不是每个依赖逐一设置。安装依赖包获取定义的依赖到你的本地项目,只需要调用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.phar 运行 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>install 命令。php composer.phar install接着前面的例子,这将会找到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>monolog/monolog 的最新版本,并将它下载到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>vendor 目录。 这是一个惯例把第三方的代码到一个指定的目录 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>vendor。如果是 monolog 将会创建 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>vendor/monolog/monolog 目录。小技巧: 如果你正在使用git来管理你的项目, 你可能要添加 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>vendor 到你的 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>.gitignore 文件中。 你不会希望将所有的代码都添加到你的版本库中。
另一件事是 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>install 命令将创建一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.lock 文件到你项目的根目录中。display:inline-block; line-height:33.75px; margin:0px 2px; background-color:rgb(249,242,244)>composer.lock - 锁文件在安装依赖后,composer 将把安装时确切的版本号列表写入 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.lock 文件。这将锁定改项目的特定版本。请提交你应用程序的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.lock (包括 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json)到你的版本库中这是非常重要的,因为 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>install 命令将会检查锁文件是否存在,如果存在,它将下载指定的版本(忽略 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json 文件中的定义)。这意味着,任何人建立项目都将下载与指定版本完全相同的依赖。你的持续集成服务器、生产环境、你团队中的其他开发人员、每件事、每个人都使用相同的依赖,从而减轻潜在的错误对部署的影响。即使你独自开发项目,在六个月内重新安装项目时,你也可以放心的继续工作,即使从那时起你的依赖已经发布了许多新的版本。如果不存在 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.lock 文件,composer 将读取 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json 并创建锁文件。这意味着如果你的依赖更新了新的版本,你将不会获得任何更新。此时要更新你的依赖版本请使用 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>update 命令。这将获取最新匹配的版本(根据你的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json 文件)并将新版本更新进锁文件。php composer.phar update如果只想安装或更新一个依赖,你可以白名单它们:php composer.phar update monolog/monolog [...]注意: 对于库,并不一定建议提交锁文件 请参考:库的锁文件.
packagistpackagist 是 composer 的主要资源库。 一个 composer 的库基本上是一个包的源:记录了可以得到包的地方。packagist 的目标是成为大家使用库资源的中央存储平台。这意味着你可以 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>require 那里的任何包。当你访问 packagist website (packagist.org),你可以浏览和搜索资源包。任何支持 composer 的开源项目应该发布自己的包在 packagist 上。虽然并不一定要发布在 packagist 上来使用 composer,但它使我们的编程生活更加轻松。自动加载对于库的自动加载信息,composer 生成了一个 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>vendor/autoload.php 文件。你可以简单的引入这个文件,你会得到一个免费的自动加载支持。require'vendor/autoload.php';这使得你可以很容易的使用第三方代码。例如:如果你的项目依赖 monolog,你就可以像这样开始使用这个类库,并且他们将被自动加载。$log=newmonolog\logger('name');$log->pushhandler(newmonolog\handler\streamhandler('app.log', monolog\logger::warning));$log->addwarning('foo');你可以在 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>composer.json 的 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>autoload 字段中增加自己的 autoloader。{ autoload: { psr-4: {acme\\: src/} }}composer 将注册一个 psr-4 autoloader 到 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>acme 命名空间。你可以定义一个从命名空间到目录的映射。此时 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>src 会在你项目的根目录,与 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>vendor 文件夹同级。例如 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>src/foo.php 文件应该包含 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>acme\foo 类。添加 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>autoload 字段后,你应该再次运行 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>install 命令来生成 display:inline-block; line-height:18px; margin:0px 2px; background-color:rgb(249,242,244)>vendor/autoload.php 文件。引用这个文件也将返回 autoloader 的实例,你可以将包含调用的返回值存储在变量中,并添加更多的命名空间。这对于在一个测试套件中自动加载类文件是非常有用的,例如。$loader=require'vendor/autoload.php';$loader->add('acme\\test\\',__dir__);除了 psr-4 自动加载,classmap 也是支持的。这允许类被自动加载,即使不符合 psr-0 规范。详细请查看 自动加载-参考。注意: composer 提供了自己的 autoloader。如果你不想使用它,你可以仅仅引入 display:inline-block; line-height:19.6875px; margin:0px 2px; background-color:rgb(249,242,244)>vendor/composer/autoload_*.php 文件,它返回一个关联数组,你可以通过这个关联数组配置自己的 autoloader。
以上就介绍了php composer安装&使用,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product