A composer is a tool for dependency management in PHP. It basically allows you to declare your project dependent libraries and helps to manage dependencies.The composer is one of the most recommended tools which is used in all modern PHP frameworks and solves fundamental issues in the projects. A Guide To Composer explains all about the composer and it’s usage.
The Composer Dependency management tool is very helpful to easily set different useful things and facilitates the installation, upgrade, and support for various libraries in your project
This article will give you an introduction to Composer and how to manage it in PHP. It will also explain what you need to change in your deployment process after integrating composer.
What is a dependency manager and Why do I need one?
Sometimes you write the code which is dependent on third party libraries, For example, you are using PHPExcel or bootstrap etc in your application and you require to update the library with new version release. All the libraries are dependent on the project so Composer allows you to manage dependencies easily and also you can upgrade all the dependent libraries easily.
All the third party tools managed through packages or we can say every dependency is for a package. Package constitutes a local file or folder, remote file or folder, local or remote Git repository etc.Most of the developers use a global registry for available packages. Packagist is most widely used for Composer.
For third party PHP library and package, instead of manually download and add them to our code base, the composer should be used instead.
There is also a Packagist repository so that you can find php packages here.
How to install Composer?
Installing Composer is pretty simple just it requires PHP 5.3 or greater version. You can follow the instructions for either *nix or Windows.
More information about composer setup can be found here: Introduction – Composer
Install/Update package with composer
To install the composer, Run the following line in your terminal to get the latest Composer version:
1 2 3 4 5 6 | php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');" |
The composer is a package manager that works according to the rules written in composer.json.
For example,
1 2 3 4 5 6 7 | { "require": { "phpoffice/phpexcel": "^1.8" } } |
composer.json is the required file to use the composer. Composer.json is mainly used to get the information about what packages you’re using in your project. Here you can see require takes an object that maps package names and version.
How to Manually Download Composer?
If you prefer to download the phar manually, you can get the latest version here https://getcomposer.org/download/
Once the composer is installed into your local development area, run composer update
to update all existing packages.This command will updates all installed (or reinstalled accidentally deleted) packages to the latest versions.
Packages will be stored in the vendor directory. We won’t track this via our repository, rather, we tracked the composer.json and composer.lock file.
To install or use new PHP lib/package, first you need to check if they are available on https://packagist.org/ and you just have to run composer require package-name to add new PHP package.
For example, find the PHPExcel package on https://packagist.org/ and then run
1 2 3 | composer require phpoffice/phpexcel |
SYNTAX:
The best way to add a new dependency to the file composer.json is to use the following command.
1 2 3 | composer require somepackage/somepackage:someversion |
This will add all the necessary dependencies to file.
If you need to add the packages to the section the require-dev , add the command option –dev :
1 2 3 | composer require phpunit/phpunit --dev |
Also, a require allows you to add multiple packages at the same time, just simply separate it by a space.
When you install new packages, the composer.json and composer.lock file will be updated. This will make sure all developers will be using the same third party packages.
More information can be found on the composer basic usage page here: https://getcomposer.org/doc/01-basic-usage.md
Load third party packages into code base to use
To load/include the third party package into the code base to use, instead of manually using PHP include to call for the library, composer provides an easy way to load the packages. Simply put:
1 2 3 | require 'vendor/autoload.php'; |
at the top of the PHP file that you are going to use the third party lib.
When installing packages Composer iterates by their rules and generates the most important file – / the vendor / autoload.php ,
Composer solves the following problems:
- To solve the dependency of PHP packages
- Provide autoload solution for PHP packages
- Easily update all packages
There is a brief overview of features.There are only two package management and autoloading classes and without the composer, both of these tasks take a lot of time.
Comments (1)