分类目录归档:Composer

Composer常用命令

  • php package仓库:https://packagist.org/
命令描述
composer search xxxx搜索包
composer show -i查看当前安装的包
composer show -a xxx/xxx显示详情
composer install安装(默认安装composer.lock)
composer install –no-dev安装(忽略require-dev)
composer update更新包(重新生成composer.lock)
composer dump-autoload生成autoload文件
composer init初始化composer项目

使用composer自动加载类文件

建立composer.json文件指定src文件夹的命名空间

    {
      "autoload":{
        "psr-4":{
          "App\\":"src/"
        }
      }
    }

生成自动加载文件

    composer dump-autoload

引入自动加载文件

    require_once 'vendor/autoload.php';
0

composer不安装dev包

通常情况下在pro环境上中安装composer包时,我们并不希望安装require-dev下面的包,以laravel的composer.json为例:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

如果是线上环境的话,我们不需要安装phpunit、mockery、faker则可以执行composer install --no-dev

0