Composer User Guide

Composer User Guide

Concept Functions
Packagist The site which contains / indexes PHP components; Public PHP components repository
Composer PHP components management tools

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:

Command Function
composer require <PHP Components> Install/Update PHP components
composer update Update PHP components
composer selfupdate Update Composer itself
composer self-update --1 Update to 1 series
composer self-update --2 Update to 2 series

Files

Below files are used by Composer:

File Functions
composer.lock Records all using components and version, this file should be in version control. composer update could update components version.
composer.json
auth.json The file where composer stores local credential information of repository
~/.composer/auth.json The file where composer stores global credential information of repository
%APPADATA%/Composer/ Global Composer configuration files reside location on Windows.

Referring

Components could be loaded in PHP files like below:

require 'vendor/autoload.php';

Common components

Component Function
nesbot/carbon DateTime extention
guzzlehttp/guzzle HTTP requests
league/csv CSV file
ircmaxell/password-compat Add password_* functions to PHP 5.5-
filp/whoops Custimized Error Page
monolog/monolog Handles application log
swiftmailer/swiftmailer Mail library, integratable with monolog
resque/php-resque Queues for background jobs

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:

Path Function
src/
tests/
composer.json Records how component is installed / loaded
README.md

composer.json

{
  "name": "",
  "description": "",
  "keywords": [""],
  "homepage": "https://",
  "license": "MIT",
  "authors": [
    {
      "name": "",
      "homepage": ""
    }
  ],
  "support": "",
  "require": {
    "": "",
    "": ""
  },
  "require-dev": {
    "": "",
    "": ""
  },
  "suggest": "",
  "autoload": {
    "psr-4": {
      "online\\moha\\": "src/"
    }
  }
}
Author: njun
njun's picture
Updated: 2022/10/02