composer入门丨技术开发分享录

composer入门

{{ detail.nickname }}

转载 翻译 {{ formatTime(detail.create_time) }} 字数 {{ detail.content && detail.content.length }} 阅读 {{ detail.read_num }} {{ formatTag(v) }}

"官方入门:[https://docs.phpcomposer.com/00-intro.html](https://docs.phpcomposer.com/00-intro.html)\n\n直接选择window全局安装就行了,下载安装程序,一步一步安装,除了选择当前的php版本外,其他全部默认\n\n安装完成后会自动把php和composer添加到系统环境变量里,这里就可以在命令行使用php和composer命令了\n\nphp\n    \n```cli\n> php -v\nPHP 7.0.12 (cli) (built: Oct 13 2016 11:04:07) ( NTS )\nCopyright (c) 1997-2016 The PHP Group\nZend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies\n```\n\ncomposer\n\n```cli\n> composer -V\nComposer version 1.8.6 2019-06-11 15:03:05\n```\n\n测试目录结构\n\n```text\n├─ composer_test\n    ├─ test.log\n    ├─ index.php\n    └─ composer.json\n```\n\ncomposer.json\n\n```json\n{\n    \"require\": {\n        \"monolog/monolog\": \"1.2.*\"\n    }\n}\n```\n\n全局修改仓库镜像([https://pkg.phpcomposer.com/](https://pkg.phpcomposer.com/))\n\n```cli\ncomposer_test> composer config -g repo.packagist composer https://packagist.phpcomposer.com\n```\n\n安装依赖\n\n```cli\ncomposer_test> composer install\n```\n\n安装完成后,会生成composer.lock文件\n\nindex.php\n\n```php\n<?php\nrequire 'vendor/autoload.php';\n\nuse Monolog\\Logger;\nuse Monolog\\Handler\\StreamHandler;\nuse Monolog\\Handler\\FirePHPHandler;\n\n// Create the logger\n$logger = new Logger('my_logger');\n// Now add some handlers\n$logger->pushHandler(new StreamHandler(__DIR__.'/test.log', Logger::DEBUG));\n$logger->pushHandler(new FirePHPHandler());\n\n// You can now use your logger\n$logger->info('My logger is now ready');\n```\n\n访问:[http://localhost/experiment/composer_test/](http://localhost/experiment/composer_test/)\n\n会在test.log里追加记录\n\n```cli\n[2019-06-18 02:08:25] my_logger.INFO: My logger is now ready [] []\n```\n\nMonolog使用示例:[https://github.com/Seldaek/monolog/blob/HEAD/doc/01-usage.md](https://github.com/Seldaek/monolog/blob/HEAD/doc/01-usage.md)\n\n**常用命令**\n\n查看项目配置\n\n```cli\n[project name]> composer config -l\n```\n\n查看全局配置\n\n```cli\n> composer config -l -g\n```\n\n安装依赖包\n\n```cli\n[project name]> composer require [package name]\n```\n\n搜索依赖包\n\n```cli\n> composer search [package name]\n```\n\n查看已安装的依赖包\n\n```cli\n> composer show\n```\n\n修改镜像\n\n```cli\n> composer config -g repo.packagist composer https://packagist.phpcomposer.com\n```\n\n| 镜像名 | 地址 |\n|: --- |: --- |\n| 阿里云 Composer 镜像 | https://mirrors.aliyun.com/composer/ |\n| 腾讯云 Composer 镜像 | https://mirrors.cloud.tencent.com/composer/ |\n| PHP 国内 Composer 镜像 | https://packagist.phpcomposer.com |\n| 华为云 Composer 镜像 | https://repo.huaweicloud.com/repository/php/ |\n| php.cnpkg.org Composer 镜像 | https://php.cnpkg.org |"
PS:写作不易,如要转裁,请标明转载出处。

如果此篇对您有帮助,可小额赞助,以兹鼓励!

猜你想看