php成长之路--1.composer下载依赖

composer

  • composer
    • 1.composer
      • 1.1 安装
      • 1.2 packagist
    • 1.3 使用composer库
  • yaf+yar企业微服务解决方案

composer

1.composer

是 PHP 用来管理依赖(dependency)关系的工具。你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件。

composer官网

1.1 安装

  • Linux / Unix / macOS系统
#下载
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

#本地安装
 php composer-setup.php --install-dir=bin --filename=composer
 #全局安装
 mv composer /usr/bin/composer
  • windows系统
    composer-setup.exe

1.2 packagist

Packagist 是主要的Composer仓库。它集合了与Composer一起安装的公开 PHP 软件包。

packagist
packagist中文网

  • packagist搜索软件包
    php成长之路--1.composer下载依赖_第1张图片
    php成长之路--1.composer下载依赖_第2张图片
    *利用composer下载guzzle库
#此过程很慢会经历几分钟
[jianghaiqiangdeMacBook-Air:composer qiang$ composer require guzzlehttp/guzzle

Using version ^6.3 for guzzlehttp/guzzle
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
  - Installing guzzlehttp/promises (v1.3.1): Downloading (100%)         
  - Installing ralouphie/getallheaders (2.0.5): Downloading (100%)         
  - Installing psr/http-message (1.0.1): Downloading (100%)         
  - Installing guzzlehttp/psr7 (1.5.2): Downloading (100%)         
  - Installing guzzlehttp/guzzle (6.3.3): Downloading (100%)         
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files

#composer生成目录及文件
[jianghaiqiangdeMacBook-Air:composer qiang$ ls
composer.json	composer.lock	vendor

#查看composer.json
[jianghaiqiangdeMacBook-Air:composer qiang$ cat composer.json 
{
    "require": {
        "guzzlehttp/guzzle": "^6.3"
    }
}

#查看vendor目录
[jianghaiqiangdeMacBook-Air:composer qiang$ ls ./vendor/
autoload.php	composer	guzzlehttp	psr		ralouphie

1.3 使用composer库

在你的入口require ./vendor/autoload.php文件即可,引入autoload.php后,vendor目录下的所有类都会依据psr-4自动加载,在我们自己的应用中可以直接使用。如:


[jianghaiqiangdeMacBook-Air:composer qiang$ vim index.php
<?php 

require __DIR__.'/vendor/autoload.php';

#即可直接使用所有vendor目录下的所有类

yaf+yar企业微服务解决方案

你可能感兴趣的:(【PHP】,php成长之路)