宽容他人,放过自己。

PHP三方管理工具-composer

Posted on By anchoriteFili

Composer安装


  1. 下载安装composer(终端命令)
    ~$ curl -sS https://getcomposer.org/installer | php
    或
    ~$ php -r "readfile('https://getcomposer.org/installer');" | php
    
  2. 将composer移动到bin中,终端就可以直接使用composer的命令了
    ~$ mv composer.phar /usr/local/bin/composer
    

Composer使用


  1. 终端进入要添加三方库的项目中
    2.png

  2. 使用命令 composer init 配置 composer.json 文件
macbookdeMacBook-Pro:test macbook$ composer init
  Welcome to the Composer config generator  
This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [macbook/test]: test
 The package name test is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+ 
Package name (<vendor>/<name>) [macbook/test]: test/test
Description []: 这是一个 composer的测试
Author [赵宏亚 <feiliyaailiya1@icloud.com>, n to skip]: 赵宏亚 <feiliyaailiya1@icloud.com>
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: 
License []: 

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? yes
Search for a package: leancloud/leancloud-sdk
Enter the version constraint to require (or leave blank to use the latest version): v0.5.6
Search for a package: 
Would you like to define your dev dependencies (require-dev) interactively [yes]? 
Search for a package: 

{
    "name": "test/test",
    "description": "这是composer的测试",
    "require": {
        "leancloud/leancloud-sdk": "v0.5.6"
    },
    "authors": [
        {
            "name": "赵宏亚",
            "email": "feiliyaailiya1@icloud.com"
        }
    ]
}

Do you confirm generation [yes]?

也可以直接手动创建composer.json文件直接里边写数据

查找三方库方法


1> 终端模糊查询sdk
composer search leancloud/leancloud-sdk

3.png

2> 查询sdk版本
composer show -all leancloud/leancloud-sdk

4.png

3> 生成了composer.json文件

5.png

4> 运行 composer install 命令下载sdk,加载完毕

6.png 7.png

5> 引入头即可使用
namespace LeanCloud;
require "vendor/autoload.php";
use LeanCloud\Client; 

8.png