Composer User Guide
Installation
Download Composer binary file to /usr/local/bin, then make it executable sudo chmod +x /usr/local/bin/composer
:
Usage
Common used Composer commands:
Files
Below files are used by Composer:
Referring
Components could be loaded in PHP files like below:
require 'vendor/autoload.php';
Common components
Configurations
# Add composer global install location to PATH.
vim ~/.profile
# set PATH so it includes composer bin if it exists
if [ -d "$HOME/.config/composer/vendor/bin" ] ; then
PATH="$HOME/.config/composer/vendor/bin:$PATH"
fi
# Let root run composer through rt
alias rt='composer --no-plugins --no-scripts'
Use China Mirror
It's difficult to use global repository in China, try below configuraiton before execute any composer command:
# Switch to Aliyun repository mirror
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# Cancel Aliyun repository mirror
composer config -g --unset repos.packagist
Or manually edit file: ~/.config/composer/config.json
, add below configurations:
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
Local proxy
If China Repo could not help composer to manage the packages more efficiently, try to install proxychains, leverage command proxychains composer
, through a proxy to boost management:
sudo apt-get install -y proxychains
vim /etc/proxychains.conf
socks5 127.0.0.1 3080
Credential
Proactivly configure credential of repository locally or globally:
composer config http-basic.<domain> <username> <password>
composer config --global http-basic.<domain> <username> <password>
auth.json
auth.json should be excluded from VCS.
{
"http-basic": {
"domain": {
"username": "",
"password": ""
}
}
Cases
Drupal 8
composer create-project drupal-composer/drupal-project:8.x-dev FOLDER_NAME --stability dev --no-interaction
Laravel
composer create-project laravel/laravel FOLDER_NAME
Creating Component
Structure
Components may include below folders and files:
composer.json
{
"name": "",
"description": "",
"keywords": [""],
"homepage": "https://",
"license": "MIT",
"authors": [
{
"name": "",
"homepage": ""
}
],
"support": "",
"require": {
"": "",
"": ""
},
"require-dev": {
"": "",
"": ""
},
"suggest": "",
"autoload": {
"psr-4": {
"online\\moha\\": "src/"
}
}
}