I. 安裝
cd path-to-your-project
curl -sS https://getcomposer.org/installer | php
# Composer successfully installed to: /tmp/composer.phar
# Use it: php composer.phar
如果在此提示,那是因為php沒有在系統的環境變數$PATH中。
command not found: php
解決方案:
為當前php的路徑建立一個軟串連
ln -s /usr/local/php/bin/php /usr/local/bin/php
仔細察看composer.phar的許可權,應該是755,即-rwxr-xr-x,說明這個檔案其他人都有執行許可權,所以如果我們將其移動至/usr/local/bin/中,即可全域可用!
mv composer.phar /usr/local/bin/composer
II. 使用
我們先使用-h查看協助
composer -h
Usage:
help [--xml] [--format="..."] [--raw] [command_name]
Arguments:
command The command to execute
command_name The command name (default: "help")
Options:
--xml To output help as XML
--format To output help in other formats (default: "txt")
--raw To output raw command help
--help (-h) Display this help message
--quiet (-q) Do not output any message
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
--no-interaction (-n) Do not ask any interactive question
--profile Display timing and memory usage information
--working-dir (-d) If specified, use the given directory as working directory.
Help:
The help command displays help for a given command:
php /usr/local/bin/composer help list
You can also output the help in other formats by using the --format option:
php /usr/local/bin/composer help --format=xml list
To display the list of available commands, please use the list command.
看來想要找到所有的命令需要運行php /usr/local/bin/composer help list
我們試試吧
php /usr/local/bin/composer list
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.0-dev (4f80e7ff68466cab970c22b425725b8058c32970) 2015-06-09 00:03:48
Usage:
command [options] [arguments]
Options:
--help (-h) Display this help message
--quiet (-q) Do not output any message
--verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
--version (-V) Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
--no-interaction (-n) Do not ask any interactive question
--profile Display timing and memory usage information
--working-dir (-d) If specified, use the given directory as working directory.
Available commands:
about Short information about Composer
archive Create an archive of this composer package
browse Opens the package's repository URL or homepage in your browser.
clear-cache Clears composer's internal package cache.
clearcache Clears composer's internal package cache.
config Set config options
create-project Create new project from a package into given directory.
depends Shows which packages depend on the given package
diagnose Diagnoses the system to identify common errors.
dump-autoload Dumps the autoloader
dumpautoload Dumps the autoloader
global Allows running commands in the global composer dir ($COMPOSER_HOME).
help Displays help for a command
home Opens the package's repository URL or homepage in your browser.
info Show information about packages
init Creates a basic composer.json file in current directory.
install Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
licenses Show information about licenses of dependencies
list Lists commands
remove Removes a package from the require or require-dev
require Adds required packages to your composer.json and installs them
run-script Run the scripts defined in composer.json.
search Search for packages
self-update Updates composer.phar to the latest version.
selfupdate Updates composer.phar to the latest version.
show Show information about packages
status Show a list of locally modified packages
update Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.
validate Validates a composer.json
III. 國內鏡像
為了更好的使用composer,我們使用phpcomposer國內鏡像加速,具體使用方法很簡單,你可以參考官網的使用說明!
IV. 在項目中使用composer
比如我們的項目需要使用laravel與monolog,怎麼辦呢?
首先,在packagist.org中搜尋需要的包名,這樣我們得到了包名monolog/monolog,laravel同理。
其次,我們需要確定版本,我們看到官網的提示:
{
"require": {
"vendor/package": "1.3.2", # 明確指定版本
"vendor/package2": "1.*", # 使用萬用字元,方便以後執行更新
"vendor/package2": "~1.3", # 是指 1.3<= X <2.0的版本
"vendor/package3": ">=2.0.3", # 指定一個範圍
"vendor/package3": ">=2.0.3-dev" # 預設composer會取穩定版,但是如果指明-dev,則會下載dev版
}
}
更多使用方法請移步Package Versions
我們在項目根目錄下建立檔案 composer.json
{
"require": {
"monolog/monolog": "1.0.*"
}
}
你還可以自動產生此composer.json,通過命令 composer init --require "monolog/monolog:1.0.*" -n