初时 composer

发表于 2020-11-13  1.18k 次阅读


Composer 是 PHP 的一个依赖管理工具。我们可以在项目中声明所依赖的外部工具库,Composer 会帮你安装这些依赖的库文件,有了它,我们就可以很轻松的使用一个命令将其他人的优秀代码引用到我们的项目中来。
Composer 默认情况下不是全局安装,而是基于指定的项目的某个目录中(例如 vendor)进行安装。

前提

本机必须要有php,并添加在环境变量里面,php.ini 的openssl拓展必须开启

网站

中文网 https://www.phpcomposer.com/
拓展包 https://packagist.org/
镜像站 官方 https://developer.aliyun.com/composer
镜像站 阿里云 https://pkg.phpcomposer.com/

使用

可能会出现的问题
  • The openssl extension is required for SSL/TLS protection but is not available
    > php.ini 未开启openssl
  • codeception/codeception[4.0.0, ..., 4.1.11] require ext-curl * -> it is missing from your system. Install or enable PHP's curl extension.
    > 安装拓展中必须有相应的拓展
  • php version
    > 当前php版本过低
正式使用
  • composer -v 查看版本
  • composer create-project --prefer-dist yiisoft/yii2-app-basic basic
  • composer install ---v (---v 显示细节)
  • composer update
  • require vendor/package:2.* vendor/package2:dev-master 加载以依赖
  • composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 使用镜像
  • composer config -g --unset repos.packagist 取消镜像
  • composer clearcache 清楚缓存
文件
  • composer.json 项目配置文件
{
    "name": "yiisoft/yii2-app-basic",  //项目名
    "description": "Yii 2 Basic Project Template",  //简介
    "keywords": ["yii2", "framework", "basic", "project template"],  //关键字
    "homepage": "http://www.yiiframework.com/",  //官网
    "type": "project",  //类型
    "license": "BSD-3-Clause",  //许可
    "support": {
        "issues": "https://github.com/yiisoft/yii2/issues?state=open",
        "forum": "http://www.yiiframework.com/forum/",
        "wiki": "http://www.yiiframework.com/wiki/",
        "irc": "irc://irc.freenode.net/yii",
        "source": "https://github.com/yiisoft/yii2"
    },
    "minimum-stability": "stable",
    "require": {  //必要依赖
        "php": ">=5.6.0",
        "yiisoft/yii2": "~2.0.14",
        "yiisoft/yii2-bootstrap": "~2.0.0",
        "yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0"
    },
    "require-dev": { //开发必要依赖
        "yiisoft/yii2-debug": "~2.1.0",
        "yiisoft/yii2-gii": "~2.1.0",
        "yiisoft/yii2-faker": "~2.0.0",
        "codeception/codeception": "^4.0",
        "codeception/verify": "~0.5.0 || ~1.1.0",
        "codeception/specify": "~0.4.6",
        "symfony/browser-kit": ">=2.7 <=4.2.4",
        "codeception/module-filesystem": "^1.0.0",
        "codeception/module-yii2": "^1.0.0",
        "codeception/module-asserts": "^1.0.0"
    },
    "config": {
        "process-timeout": 1800,
        "fxp-asset": {
            "enabled": false
        }
    },
    "scripts": {  //安装项目时脚本,对应下面
        "post-install-cmd": [ 
            "yii\\composer\\Installer::postInstall"
        ],
        "post-create-project-cmd": [
            "yii\\composer\\Installer::postCreateProject",
            "yii\\composer\\Installer::postInstall"
        ]
    },
    "extra": {
        "yii\\composer\\Installer::postCreateProject": {
            "setPermission": [
                {
                    "runtime": "0777",
                    "web/assets": "0777",
                    "yii": "0755"
                }
            ]
        },
        "yii\\composer\\Installer::postInstall": {
            "generateCookieValidationKey": [
                "config/web.php"
            ]
        }
    },
    "repositories": [
        {
            "type": "composer",
            "url": "https://asset-packagist.org"
        }
    ]
}

  • composer.lock (composer install 生产此文件,配置锁文件 保证项目版本不变 composer install检测此文件,有则使用此文件的信息,没有则使用 composer.json 文件信息)
  • vendor 放置所有拓展的文件夹
  • vendor/composer 放置所有拓展的自动加载配置
自动加载类
require __DIR__ . '/../vendor/autoload.php';

小知识:
include 警告
require 致命错误

本站文章基于国际协议BY-NA-SA 4.0协议共享;
如未特殊说明,本站文章皆为原创文章,请规范转载。

0

一盏灯 一座城 找一人 一路的颠沛流离