composer是php的依賴管理工具,是很是有用的,phper的必備技能。 可是可能由於出現的比較晚,使用的人仍是是太少,網上竟然找不到像樣的入門文章,要麼複製黏貼,要麼直接列一堆命令,官方文檔比較詳細了,可是頁面和內容組織都比較通常,因此我來寫一個。php
在咱們的項目中,可能用到別人的包,之前咱們回去下載下來放到咱們的項目中,在代碼中再require,其實這是比較low的,比較麻煩 composer就是自動管理依賴的工具,你只要在你的項目中聲明依賴哪些包,composer就會自動去下載,就這樣。 另外,composer還有一個自動生成autoload文件的便利功能。html
win的話下載一個安裝文件,安裝,就在命令行用了,全局的。 其餘平臺看官方文檔,文章最後有官網連接。mysql
安裝的過程當中我遇到了錯誤 報錯 Failed to decode zlib stream 解決辦法是在php.ini配置文件中將 zlib.output_compression=Off 改爲 Onlaravel
命令 | 備註 |
---|---|
composer init | 初始化項目 引導生成composer.json |
composer search | 搜索包 |
composer require | 安裝新的依賴包 |
composer update [package name] | 更新依賴 |
包 包就是一個文件夾,對項目的意義就是一個能夠引用的組件,好比monologgit
composer composer其實就是一個phar文件,當作一個工具來使用,全局安裝了的話就composer install這樣用,沒有全局安裝就 php /path/to/composer.phar install這樣用github
packgist 咱們須要一個存儲包的中央倉庫,這樣只要告訴composer一個包的名字,composer就會從這個中央倉庫去下載代碼,https://packagist.org是 Composer 的主官方資源庫。 能夠訪問 packagist website (https://packagist.org/) (packagist.org)瀏覽和搜索資源包。web
若是你在你的項目中使用composer,會增長兩個文件sql
因此提交你的項目的時候 這兩個文件都要提交mongodb
當別人下載你的項目,composer會先查看有沒有lock文件,若是有,就按照lock文件下載指定的依賴,這樣別人跟你的項目的依賴的全部版本都會一致npm
圍繞着依賴管理,天然會有這麼幾個操做
咱們能夠手動建立json文件,也能夠自動建立,有自動固然首選自動 在咱們的項目根目錄運行命令行composer init
就自動建立了
PS D:\code\test3> composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [kelle/test3]: my/ctest
Description []:
Author [, n to skip]: dragonfly429 <dragonfly429@foxmail.com>
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []:
License []:
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]?
Search for a package:
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package:
{
"name": "my/ctest",
"authors": [
{
"name": "dragonfly429",
"email": "dragonfly429@foxmail.com"
}
],
"require": {}
}
Do you confirm generation [yes]?
複製代碼
其中這個json文件的字段和格式須要瞭解一下
--name: 包的名稱。
--description: 包的描述。
--author: 包的做者。
--homepage: 包的主頁。
--require: 須要依賴的其它包,必需要有一個版本約束。而且應該遵循 foo/bar:1.0.0 這樣的格式。
--require-dev: 開發版的依賴包,內容格式與 --require 相同。
--stability (-s): minimum-stability 字段的值。
複製代碼
name author require 是必填的 -dev表明開發時纔用到的 這個跟npm的語法都同樣的(save 不用寫)
插一個搜索命令composer search
PS D:\code\test3> composer search monolog
monolog/monolog Sends your logs to files, sockets, inboxes, databases and various web services
symfony/monolog-bundle Symfony MonologBundle
symfony/monolog-bridge Symfony Monolog Bridge
easycorp/easy-log-handler A handler for Monolog that optimizes log messages to be processed by humans instead of software. Improve your productivity with logs that are easy to understand.
wazaari/monolog-mysql A handler for Monolog that sends messages to MySQL
theorchard/monolog-cascade Monolog extension to configure multiple loggers in the blink of an eye and access them from anywhere
logentries/logentries-monolog-handler A handler for Monolog that sends messages to Logentries.com.
flynsarmy/slim-monolog Monolog logging support Slim Framework
bramus/monolog-colored-line-formatter Colored Line Formatter for Monolog
tylercd100/lern LERN (Laravel Exception Recorder and Notifier) is a Laravel 5 package that will record exceptions into a database and will notify you via Email, Pushover or Slack.
maxbanton/cwh AWS CloudWatch Handler for Monolog library
rahimi/monolog-telegram A handler for Monolog that sends messages to Telegram Channels
markhilton/monolog-mysql Laravel 5 MySQL driver for Monolog
lexik/monolog-browser-bundle This Symfony2 bundle provides a Doctrine DBAL handler for Monolog and a web UI to display log entries
kdyby/monolog Integration of Monolog into Nette Framework
複製代碼
命令composer require
PS D:\code\test3> composer require monolog/monolog
Using version ^1.23 for monolog/monolog
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing psr/log (1.0.2): Downloading (100%)
- Installing monolog/monolog (1.23.0): Downloading (100%)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver)
monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib)
monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server)
Writing lock file
Generating autoload files
複製代碼
完成後會多一個vendor文件夾,包含有monolog和autoload
額 而後 喜聞樂見的下載很慢 添加國內鏡像是基本操做
引用知乎回答
Composer 下載擴展包時候須要跟這兩個網站通訊:Packagist 官網 —— 獲取擴展包信息,下載代碼包;GitHub ——
下載代碼包。下載慢的緣由是這兩個網站都爲國外的,通常狀況下訪問速度很慢,有時候甚至沒法訪問。解決方案是將構建一臺能高速訪問的服務器,並將全部的擴展包使用國內的 CDN 進行加速。咱們社區維護了一個加速鏡像,請見:Laravel China 社區維護的國內全量鏡像 ——
https://laravel-china.org/composer使用方法:
選項1、全局配置(推薦)
`$ composer config -g repo.packagist composer https://packagist.laravel-china.org`
選項2、單獨使用若是僅限當前工程使用鏡像,去掉 -g 便可,以下:
`$ composer config repo.packagist composer https://packagist.laravel-china.org`
取消鏡像
`composer config -g --unset repos.packagist`
做者:Summer
連接:https://www.zhihu.com/question/24997679/answer/30703365
來源:知乎
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
複製代碼
remove和 update
略
略
·install
就是下載一個包的時候 這個命令是讓composer讀取composer.json,安裝裏面列出的依賴
只須要將下面這行代碼添加到你項目的引導文件中:
require 'vendor/autoload.php';
require 'vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('./test.log', Logger::WARNING));
// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');
複製代碼
運行 多了一個文件 成功
使用GitHub、Composer、Packagist管理公開的PHP包(Step By Step)