1、composer,既然你看这篇文章,说明你已经使用或者即将使用composer。关于composer的安装环境就不再过多描述。(直接下载安装即可)
2、composer,是什么。
其实,可以把composer理解为php的“360软件管家”就行了,或者apt-get 包管理工具,再或者理解为“手机应用市场”。(composer是:php的类库管理软件)
3、如何使用composer
网址: www.packagist.org
查找我们要使用的软件
比如,
然后,看captcha的说明,来使用
$ vi composer.json
{
"require": {
"gregwar/captcha": "1.*"
}
}
切换到composer.json脚本所在的目录, 执行 composer install 命令
vagrant@homestead:~/Code/demo$ composer install
No lock file found. Updating dependencies instead of installing from lock file. Use composer update over composer install if you do not have a lock file.
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
- Locking gregwar/captcha (v1.1.9)
- Locking symfony/finder (v5.3.7)
- Locking symfony/polyfill-php80 (v1.23.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
- Downloading symfony/finder (v5.3.7)
- Downloading gregwar/captcha (v1.1.9)
- Installing symfony/polyfill-php80 (v1.23.1): Extracting archive
- Installing symfony/finder (v5.3.7): Extracting archive
- Installing gregwar/captcha (v1.1.9): Extracting archive
Generating autoload files
2 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
然后查看当前目录
-rw-rw-r-- 1 vagrant vagrant 54 Sep 15 00:57 composer.json
-rw-rw-r-- 1 vagrant vagrant 8324 Sep 15 00:58 composer.lock
-rw-rw-r-- 1 vagrant vagrant 187 Sep 15 01:17 index.php
drwxrwxr-x 5 vagrant vagrant 4096 Sep 15 01:11 vendor/
你会发现多了2几个文件
4、使用安装的软件包
我们先看文档
在composer.json脚本的同级目录, 编写index.php文件
include './vendor/autoload.php';
use Gregwar\Captcha\CaptchaBuilder;
$builder = new CaptchaBuilder;
$builder->build();
header('Content-type: image/jpeg');
$builder->output();
然后通过浏览器浏览一下
就看到验证码了
//到此,composer 怎么使用就所说明白了
执行Composer Install时,Composer会读取当前目录下的composer.json脚本,并解析json格式的数据,获取require里的软件安装列表,去对应的软件库中下载指定的项目代码。