composer

安裝composer,在Linux上。

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

第二步:安裝完成之後輸入

# composer -v
顯示如下說明成功。
Do not run Composer as root/super user! See https://getcomposer.org/root for details
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.5.2 2017-09-11 16:59:25

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -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.
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

第二步:接下來進入項目目錄

初始化composer

composer init
出現:
Do not run Composer as root/super user! See https://getcomposer.org/root for details

                                            
  Welcome to the Composer config generator  
                                            


This command will guide you through creating your composer.json config
Package name (<vendor>/<name>) [root/test]: 輸入名字
Description []: 輸入描述
Author [, n to skip]:作者信息
Minimum Stability []: stable  資源包的最低穩定版本,默認爲 stable
Package Type (e.g. library, project, metapackage, composer-plugin) []: 選擇你的類型:庫、項目。一般用project
License []: 協議 MIT
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
{
    "name": "mly/test",
    "description": "a test",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "mly",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "require": {}
}
Do you confirm generation [yes]?生成上面的文件? yes
在目錄下就生成了composer.json文件



切換到中國鏡像,外國網站太慢

composer config repo.packagist composer https://packagist.phpcomposer.com  在composer.json所在目錄下執行

接下來

composer install


在當前目錄下生成了一個文件夾vendor

vendor目錄結構:
vendor----autoload.php
          ----composer     ----autoload_classmap.php
                                  ----autoload_namespaces.php
                                  ----autoload_psr4.php(自動加載)
                                  ----autoload_real.php
                                  ----autoload_static.php
                                  ----ClassLoader.php
                                  ----installed.json
                                  ----LICENSE


第三:文件自動加載:例如thinkPHP的function.php的加載

回到項目目錄,新建一個common目錄,在common下創建function.php文件,隨便寫個方法。

<?php
function test($str){
    return md5($str);
}

然後到項目中,打開composer.json文件,添加紅色部分,中括號裏面就是要引入文件的路徑,需要引入其他文件的時候在中括號中加“,”繼續寫上路徑就可以,之後執行composer dump


{
    "name": "mly/test",
    "description": "a test",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "mly",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "require": {},
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
    "autoload":{
        "files":["common/function.php"]
    }
}


然後在項目中需要的地方引入vendor下的autoload.php就可以使用。


自動加載類:

{
    "name": "mly/test",
    "description": "a test",
    "type": "project",
    "license": "MIT",
    "authors": [
        {
            "name": "mly",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "require": {},
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://packagist.phpcomposer.com"
        }
    }
    "autoload":{
        "files":["common/function.php"]//文件的自動加載
        "psr-4":{"類的命名空間要雙斜槓(App\\Home\\)":"文件路徑(相對於composer.json的)"}
    }
}

之後運行composer dump

用到的地方引入autoload.php 然後實例化類就可以了(實例化的時候要加上那個類的命名空間或者use那個命名空間)psr-4:類名要和文件名一樣

如果要用其他的工具類或者別人寫好的類或功能

訪問:https://packagist.org/  可以在裏面搜索,選擇★高的或者下載量大的可能bug比較少,並且有使用文檔。

在項目中運行 composer require +你查找到的類,完成之後到vendor目錄下就會看到多了剛纔下載的文件

然後在項目中引入autoload.php 實例化就可以用了




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章