composer入门

2019-06-18  本文已影响0人  小程序前端超市

官方入门:https://docs.phpcomposer.com/00-intro.html

直接选择window全局安装就行了,下载安装程序,一步一步安装,除了选择当前的php版本外,其他全部默认

安装完成后会自动把php和composer添加到系统环境变量里,这里就可以在命令行使用php和composer命令了

php

> php -v
PHP 7.0.12 (cli) (built: Oct 13 2016 11:04:07) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

composer

> composer -V
Composer version 1.8.6 2019-06-11 15:03:05

测试目录结构

├─ composer_test
    ├─ test.log
    ├─ index.php
    └─ composer.json

composer.json

{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}

全局修改仓库镜像(https://pkg.phpcomposer.com/

composer_test> composer config -g repo.packagist composer https://packagist.phpcomposer.com

安装依赖

composer_test> composer install

安装完成后,会生成composer.lock文件

index.php

<?php
require 'vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;

// Create the logger
$logger = new Logger('my_logger');
// Now add some handlers
$logger->pushHandler(new StreamHandler(__DIR__.'/test.log', Logger::DEBUG));
$logger->pushHandler(new FirePHPHandler());

// You can now use your logger
$logger->info('My logger is now ready');

访问:http://localhost/experiment/composer_test/

会在test.log里追加记录

[2019-06-18 02:08:25] my_logger.INFO: My logger is now ready [] []

Monolog使用示例:https://github.com/Seldaek/monolog/blob/HEAD/doc/01-usage.md

写到最后,欢迎关注作者:http://fenxianglu.cn/

上一篇 下一篇

猜你喜欢

热点阅读