May 25, 2021 Composer
Composer is a PHP-dependent management tool. It allows you to state the code base on which your project depends, and it installs them for you in your project.
Composer is not a package manager. Y
es, it involves "packages" and "libraries," but it is managed on a per-project basis and installed in a directory
vendor
such as vendor. B
y default, it does not install anything globally.
Therefore, this is just a dependency management.
This idea is not new, and Composer was strongly inspired by node's (npm) and ruby's 'bundler' . At that time, there were no similar tools under PHP.
Composer will solve the problem for you like this:
a) You have a project that relies on several libraries.
b) Some of these libraries depend on others.
c) You declare what you depend on.
d) Composer will find out which version of the package needs to be installed and install them (download them to your project).
Let's say you're creating a project and you need a library to do logging. Y
ou decide to use the "monolog" .
To add it to your project, all you need to do is create a
composer.json
that describes the project's dependencies.
{
"require": {
"monolog/monolog": "1.2.*"
}
}
Let's just point out that our project
monolog/monolog
packages,
1.2
Running Composer requires PHP 5.3.2 plus or more. Some sensitive PHP settings and compilation flags are also required, but warnings are thrown for any incompatible installers.
Instead of simply downloading zip files, you'll need git, svn, or hg, depending on the version management system you load the package with.
Composer is multi-platform, and we're trying to make it work just as well on Windows, Linux, and OSX platforms.
To really get Composer, we need to do two things. Install Composer first (again, which means it will be downloaded to your project):
curl -sS https://getcomposer.org/installer | php
Note:
If the above method fails for some reason, you can also download the installer through
php
php -r "readfile('https://getcomposer.org/installer');" | php
This checks some PHP settings and
composer.phar
into your working directory. T
his is Composer's binary.
This is a PHAR package (archive of PHP), which is the archive format of PHP that helps users do something on the command line.
You can specify composer's installation directory (it can be an absolute or relative path) by using the option of
\--install-dir
curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
You can put this file anywhere. I
f you put it in the
PATH
directory, you can access it globally.
In a Unix-like system, you can even use it without
php
You can execute these commands to
composer
make global calls in your system:
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Note: If the appeal command fails because of permission execution, try running
mv
using sudo.
Now you only need
composer
command to use composer without having to enter
php composer.phar
Composer is part of the homebrew-php project.
brew update
brew tap josegonzalez/homebrew-php
brew tap homebrew/versions
brew install php55-intl
brew install josegonzalez/php/composer
This is the easiest way to install Composer on your machine.
Download and run the composer-Setup.exe, which installs the latest version of Composer and sets the system's environment variables, so you can use the composer command directly in
composer
Set the system's environment
PATH
and run the installation command to download the composer.phar file:
C:Usersusername>cd C:bin
C:bin>php -r "readfile('https://getcomposer.org/installer');" | php
Note:
If you receive a readfile error prompt,
http
link or turn .ini on the php php_openssl.dll.
Create
composer.phar
peer
composer.bat
C:bin>echo @php "%~dp0composer.phar" %*>composer.bat
Close the current command-line window and open a new command-line window to test:
C:Usersusername>composer -V
Composer version 27d8904
Now we'll use Composer to install the dependencies of the project.
If you don't have
composer.json
in the current directory, check out the basic usage section.
To resolve and download dependencies, execute
install
command:
php composer.phar install
If you have a global installation and do not have a phar file in the current directory, use the following command instead:
composer install
Continuing with the example above, the monolog will be
vendor/monolog/monolog
directory.
In addition to the library download, Composer has an automatic loading file that loads all the class files in the library downloaded by Composer. With it, you only need to add the following line of code to your project's boot file:
require 'vendor/autoload.php';
Now we can use monolog! To learn more about Composer, check out the Basic Usage section.