Article directory
1. Introduction to Composer
For modern languages, package managers are basically standard. Java has Maven, NodeJs has NPM, Objective-C has CocoaPods, PHP has PEAR, etc.
But PEAR has the following disadvantages:
- Dependency handling is prone to problems
- Configuration is very complicated
- Difficult command line interface
So there is Composer:
- Composer is a dependency management tool for PHP, not a package manager. It deals with "packages" and "libraries"
- Declare dependent external tool libraries (libraries) in the project, Composer will automatically install these tool libraries and dependent library files
For example, in the figure below, the A extension in the project is based on the B and C extensions. If you want to use the A extension, then these three extensions must be installed manually. If you use Composer to configure the A extension, then Composer will automatically install A's dependencies B and C extensions.
Composer is not a package manager like Yum or Apt. Although it handles packages or libraries, it manages these packages and libraries based on the project. It installs these packages and libraries into a directory of your project (usually in vendor folder), which is not installed globally by default. As a package manager, it also supports a global command called "global", we can use this command to install global packages.
This extension package management method is not the first or exclusive patent of Composer. In fact, Composer is largely inspired by node's npm and ruby's bundler.
2. Install Composer
Compser needs to be installed on PHP 5.3.2+ and can run on Windows, Linux and OSX platforms.
1. Linux installation
composer.phar
Linux is installed by downloading the Composer executable and running the executable
Composer provides a convenient installer that you can execute directly from the command line. download link
There are two ways to download composer.phar:
-
Direct download: https://getcomposer.org/download/
-
Command line download
Command line download
Execute the following command on the command line to download:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
The composer-setup.php file will be generated in the current directory
Then run:
php composer-setup.php
This command will check whether the current PHP.ini configuration meets the conditions. If so, the composer.phar file will be downloaded. After the download is complete, the composer-setup.php file can be deleted.
The downloaded composer.phar can be installed in two ways:
- Local installation: Copy the composer.phar file to any directory (such as the project root directory), and then use the php composer.phar command to use composer.
- Install globally:
mv composer.phar /usr/local/bin/composer
Use in cmd composer --version
to verify
2. Windows installation
There are two ways to install Composer:
- Installer: Download and run Composer-Setup.exe
- composer.phar: general installation method, please refer to Linux installation
Installer
Download and run Composer-Setup.exe , it will install the latest version of Composer, then set the system environment variables
Note: Openssl configuration needs to be enabled, open php.ini in the php directory, and remove the extension=php_openssl.dll
preceding semicolon.
Note: If you download the composer.phar file for installation, the Windows system needs to copy the composer.phar to the same directory as php.exe, create a new composer.bat file, and save the following code to the file:
@php "%~dp0composer.phar" %*
3. Update Composer
If you already have Composer installed and you just want to update, you can do it with the following command:
composer selfupdate
3. Composer acceleration
Since the connection speed of foreign websites is very slow and may be blocked at any time, domestic mirrors are generally used for acceleration, such as Alibaba Cloud Compser full mirror or Composer China full mirror.
What Composer China Full Mirror and Alibaba Cloud Compser Full Mirror do is to cache all installation packages and metadata to the domestic computer room and accelerate it through the domestic CDN, so that there is no need to go to foreign websites to initiate requests.
View the current composer global configuration:
composer config -gl repo.packagist
As shown in the figure below, where the arrow points to the mirror address:
You can enable domestic mirror acceleration for a project, or you can configure Composer globally
Configure a single project as an Alibaba Cloud image:
Execute cmd in the project directory:
composer config repo.packagist https://mirrors.aliyun.com/composer/
Then open composer.json, the content will change:
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
}
Configure global variables as Alibaba Cloud images:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
Then check the mirror address currently in use.
4. Using Composer
The main commands of Composer are:
- search
- show
- Declare dependencies (require)
- install
- update
- Remove dependencies (remove)
1. Initialize
Create a new directory abcd, then enter the directory in cmd, and initialize it first:
composer init
Enter the name of the package (in the format: xxx/xxx), then enter the description, then enter the author and email (in the format: John [email protected] ), then enter the version (can be skipped), enter the License (can be skipped) , and then whether to define dependencies, you can skip it first
as follows:
After that, you can view composer.json in the current directory:
{
"name": "jiand/abcd",
"autoload": {
"psr-4": {
"Jiand\\Abcd\\": "src/"
}
},
"authors": [
{
"name": "xxx",
"email": "[email protected]"
}
],
"require": {
}
}
Note: composer init
The purpose is to generate the composer.json, or you don't need this command, just write the composer.json directly, and the content can be very concise:
{
}
2. Search
For example, to search for a php library Monolog, which is a logging tool.
Execute directly under cmd:
composer search monolog
You can see that there is a monolog/monolog, which is the desired tool
3. View
View installed packages or dependencies
composer show
View information such as the version of a package:
composer show --all monolog/monolog
as follows:
name : monolog/monolog
descrip. : Sends your logs to files, sockets, inboxes, services
keywords : log, logging, psr-3
versions : dev-master, 2.0.x-dev, 1.x-dev, 1.22.1, 1.20, 1.18.2, 1.18.1, 1.18.0, 1.17.2, 1.17.1, 1.17.0, 1.11, 1.13.0, 1.12.0, 1.11.0, 1.10.0, 1.9.1, 1.9.0, 1.8.0.1, 1.4.0, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.2, 1
type : library
license : MIT License (MIT) (OSI approved) https://spicenseText
source : [git] https://github.com/Seldaek/monolog.gid7c0a4e490729614
dist : [zip] https://files.phpcomposer.com/files/S9cb36f17b1cc2d7c0a4e490729614.zip 35c84c5b6f79cb36f17b
names : monolog/monolog, psr/log-implementation
4. Declaration of Dependence
Two ways:
- add to composer.json
- Direct command:
composer require
add to composer.json
Add monolog/monolog to composer.json:
{
"name": "jiand/abcd",
"autoload": {
"psr-4": {
"Jiand\\Abcd\\": "src/"
}
},
"authors": [
{
"name": "xxx",
"email": "[email protected]"
}
],
"require": {
"monolog/monolog": "1.26.*"
}
}
Then install it under cmd:
composer install
In this way, the vendor directory will be generated in this directory, and there will be downloaded dependencies in it.
Direct command to declare dependencies [ recommended ]
In this way, you can even do it directly without the composer.json file, and the composer require
composer.json will be automatically generated
composer require symfony/http-foundation
After running, the extension will be automatically added to composer.json:
{
"name": "jiand/abcd",
"autoload": {
"psr-4": {
"Jiand\\Abcd\\": "src/"
}
},
"authors": [
{
"name": "xxx",
"email": "[email protected]"
}
],
"require": {
"monolog/monolog": "1.26.*"
}
}
And the symfony/http-foundation extension will be added to the vendor.
It is also possible to declare version constraints for a dependency:
composer require php-amqplib/php-amqplib:2.*
5. Update dependencies
You can update dependencies if:
- delete a library
- Change the version of a dependency
- add dependency
- update all dependencies
- update a dependency
For example, delete symfony/http-foundation in require in composer.json, and then execute
composer update
to remove the extension
For example, to update all dependencies under the project, you can also use
composer update
Note: The version that the package can upgrade is subject to the version constraint, and the package will not be upgraded to the range of the version beyond the constraint. For example, if the version constraint of the package in composer.json is ^1.10, and the latest version is 2.0. Then the update command cannot upgrade the package to version 2.0, but can only upgrade to version 1.x.
For example, update a dependency under the project:
composer update symfony/http-foundation
6. Remove dependencies
A dependency can composer remove
be removed by a command, such as:
composer remove symfony/http-foundation
Five, Composer installs Laravel
There are two ways:
- direct installation
- Laravel Installer
direct installation
Install Laravel via the Composer Create-Project command:
composer create-project laravel/laravel --prefer-dist [文件夹名]
You can also install a specific version of Laravel:
composer create-project --prefer-dist laravel/laravel [文件夹名] 5.8.*
Laravel installer installation
First install the Laravel installer with the following command:
composer global require "laravel/installer"
Then add the environment variable of the system, and add ~/.composer/vendor/bin
the path to the path, so that you can use the laravel command.
Create a Laravel project:
laravel new [文件夹名]
If you want to create the latest development version of your Laravel project, use:
laravel new [文件夹名] --dev
The projects created above can be accessed in the browser through the domain name/folder name/public.