Magento 2 Create composer.json

Creating composer.json in Magento 2 brings you more and more convenience to manage components better that used in your project. This topic is the well-documented instruction to assist the store owners to be familiar with the composer.json file.

Overview of Magento 2 Composer

Composer is a dependency management for PHP and in Magento 2, the composer plays the role in gathering components and product editions. When using the Magento 2 composer, there is no requirement of storing all code of each component, and you only need to create composer.json file and declare the needed components in that file instead that. Immediately, the composer will auto-define and take the corresponding data of those components to run for your project.

Here are some situations of the Magento 2 components including composer.json file:

  • When a component contains composer.json file, and needs to install via Magento 2 composer as well (like from Packagist, the Magento Marketplace, or another source), the Component Manager can update, uninstall, enable, or disable the component.

  • When a component still includes composer.json file but doesn’t need to install via Magento 2 composer such as the customization of written codes by a developer, the Component Manager can still enable or disable the component.

So, it is easier for you to run the project smoothly when your component’s root directory is added the composer.json file.

Explanation of composer.json files

Both Magento 2 components and product editions can work with composer.json files, specifically:

Root

  • Location: composer.json

  • Name: magento/magento2ce

  • Type: project

This is the main composer.json file. It can be used as a template for every root composer.json file to call dependencies on third-party components.

CE project

  • Location: composer.json

  • Name: magento/project-community-edition

  • Type: project

This file is used for the Magento Community Edition project. It allows calling the dependencies on the Magento product and the class autoloader. In addition, you can use it to deploy Magento using Composer via Magento system integrators.

CE product

  • Location: composer.json

  • Name: magento/product-community-edition

  • Type: metapackage

This file is used for the Magento Community Edition product. It allows calling the dependencies on the Magento components (modules, themes, and so on) and third-party components. In addition, you can use it to deploy Magento using Composer via Magento system integrators.

Magento Framework

  • Location: lib/internal/Magento/Framework/composer.json

  • Name: magento/framework

  • Type: magento2-library

You only used this file for Magento framework.

Module

  • Locations:

app/code/<vendor-name>/<module-name>/composer.json

vendor/<vendor-name>/<module-name>/composer.json

  • Name: <vendor-name>/<package-name>

  • Type: magento2-module

The composer.json file will aid a module extension to call external dependencies when it needs to be functioned.

Theme

  • Locations:

app/design/frontend/<vendor-name>/<theme-name>/composer.json

app/design/adminhtml/<vendor-name>/<theme-name>/composer.json

  • Name: <vendor-name>/<package-name>

  • Type: magento2-theme

For a theme component, this file consists of parent theme dependencies which are used for the inheritance of the extension.

Language Package

  • Location: app/i18n/<vendor-name>/<language-code>/composer.json

  • Name: <vendor-name>/<package-name>

  • Type: magento2-language

The composer.json file for language packages have to include the correct ISO code that is suitable for the language code.

Kinds of the component in Magento 2 Composer

There are many kinds of Magento 2 component like a module, theme, or language package. However in the case of not being assigning to an exact category, that will be called generally as magento-2-component.

To determine the type of Magento 2 component, the system possibly combines the directories and files to the correct locations, that is based on the Magento 2 directory structure.

Naming conventions of the component in Magento 2 Composer

When creating Magento 2 composer, you are compulsory to use the format of the namespace: <vendor-name>/<package-name>. Thus, in this part, we will learn the conventions of the vendor-name and package-name.

vendor-name

The vendor-name must be lowercase as the following format for Magento 2 extensions: magento2/*

In case that you want to submit the extension to Magento Marketplace, you have to use the name you have registered the account there. That name is injected into the composer.json file as the vendor-name part of the extension name. I have a specific illustrator for your approach:

 {
  "name": "mageplaza/magento-2-seo-extension",
  "description": "Magento 2 SEO extension",
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "mageplaza/core-m2": "dev-master"
  },
  "type": "magento2-module",
  "version": "1.1.1",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "authors": [
    {
      "name": "Mageplaza",
      "email": "[email protected]",
      "homepage": "https://www.mageplaza.com",
      "role": "Leader"
    }
  ],
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Mageplaza\\Seo\\": ""
    }
  }
}

package-name

The package-name also contains the lowercase with multiple words, so Magento 2 Composer asks for separating them by dash as the following convention:

magento2/<type-prefix>-<suffix>[-<suffix>]...

In particular, type-prefix is a type of Magento 2 component and suffix is a thing to realize which component it is within that type.

Conclusion

That is all things you need to pay attention when creating a composer.json file in Magento 2. Please follow carefully and make yourself more comfortable with the composer.json file when running your projects.

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 create composer.json
  • Magento 2 Create composer file
  • magento 2 composer create dar file
  • could not create file magento 2 composer.phar
  • create composer file for magento 2
  • create a magento 2 composer json file
  • please create a magento 2 composer json file
  • magento 2 composer update config file
  • create hex file code composer magento 2
  • install magento 2 composer.json file
  • magento 2 composer update lock file only
  • 2.3.x, 2.4.x
x