What is Composer? Why it is required?
Composer is an application level package-manager for PHP and Magento 2. It provides a standard format to manage dependencies of Magento 2 application and required libraries.
Composer allows developers to install PHP applications that are available on “Packagist”. It also provides autoload capabilities for libraries.
For magento2 composer can update, uninstall, enable, or disable an extension if installed using Composer (including from Packagist, Magento Marketplace, or another source) and it has a composer.json file. The Component Manager can still disable or enable an extension not installed using Composer (e.g. custom code) if it has a composer.json file. (For more details, refer: https://devdocs.magento.com/guides/v2.0/extension-dev-guide/build/composer-integration.html)
How to use composer in Magento 2?
To create a module that supports composer based installation the only required step is to create a composer.json file for it.
Step 1: Create composer.json file for any module
Create new composer.json file in location app/code/<vendor-name>/<module-name>/composer.json
Required information for this file:
name- It should be component name, format is <vendor-name>/<component-name>.
All letters must be in lowercase. You can use dashes in the <component-name> to separate words.
type- For a new module, this value must be set to magento2-module.
Other possible types are project, metapackage, magento2-library, magento2-theme, and magento2-language.
autoload- Specify necessary information to be loaded, such as registration.php.
Step 2: Create and Validate the package
Create a package of your module by performing a zip operation on the directory with your extension.
A sample command can be as:
zip -r ezest-digital-commerce_customer-loyalty-1.1.1.zip package-path/ -x 'package-path/.git/*'
(Use alphanumeric characters for the package filename with dashes to separate words. Do not use whitespaces.)
Magento can retrieve your extension package from any valid GitHub URL.
Third party repositories are also supported. For that add the following to your composer.json file:
Use validation tool to verify your package.
Your package is ready to publish and destitute.
Stay tuned for my next blog in which we will learn how to install any module using Composer in Magento 2.