The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

6 Steps to Creating an Extension Package File in Magento

Magento is considered one of the most common open-source eCommerce platforms for the SME segment. It provides amazing features and functionalities that meet most of the requirements of the eCommerce industry.

Although various domains have the same basic features and functionalities, they need customizations to meet the customers’ specific requirements in their domain.

These requirements are met through extensions which are adjustments in the Magento’s core, or they are extensions of the current functionalities.

When extensions are generated, they need packaging and submitting to Magento Connect for approval. Only when approved, these extensions are shown in the Magento Connect.

To generate a package, developers will need a Magento Extension Package file.

In this post today, we’re going to show you detailed instructions to generate an Extension Package File in Magento.

Table of contents

How to Create a Magento Extension Package File

Step 1: Generate the module folder

Magento provides two possible locations for modules: the app/code folder and the vendor folder. According to how Magento is installed, core modules can appear in the vendor/magento/magento- folders (for composer installation) or in the app/code/Magento/ folder (for cloning GitHub).

If you create a file for a particular project, it is crucial to select the app/code folder and follow the repository of the project.

If you create an extension file to be reused, you should utilize the composer to build it, and enter your module in the vendor **/ /** module-something folder.

Every module name in Magento 2 contains two parts - the vendor and the module. That means modules are classified into vendors, so you have to determine the vendor and module names. For this instance, call the vendor “Learning” and the module “FirstUnit”.

Now let’s build the folder app/code/Learning and inside this folder place another folder: FirstUnit.

Step 2: Generate an etc/ module.xml file

This file is needed for the module to exist. The file consists of the information below:

  • Module name
  • Module version
  • Dependencies

The module name is determined by the folders we just generated, as in Magento 2, class names must obey the folder structure. As we built the folders Learning/ FirstUnit, our module name will be Learning_FirstUnit, and all classes included in this module will start with Learning\FirstUnit – for example: Learning\FirstUnit\Observer\Test.

The module version shows the current version of the database schema and data and is utilized in upgrading. For instance, let’s say you choose to modify a table’s schema in your module. How can you make sure that this change will occur in all cases where the code is implemented? Changing the database by direct SQL queries won’t work.

Instead, Magneto has installed and updated scripts in each module. These scripts include commands to adjust the database schema or data. To keep track of whether to execute a script or not, Magento uses module versions. Whenever you make a new database change, you deploy a new version of a module and modify the corresponding module.xml. Magento saves the current version in a database, and if the database and the one in the module.xml don’t match, it will implement the upgrade code.

Dependencies

If one module relies on another, the module.xml file will have a declaration that determines a set of modules that the current module relies on. For instance, we will make your module depend on Magneto_Catalog.

Applying the command-line code below, build the folder app/ code/ Learning/ FirstUnit/etc:

$ mkdir app/ code/ Learning/ FirstUnit/etc

Next, enter the code below in it:

1 <?xml version="1.0"?>

2 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

3 <module name="Learning_FirstUnit" setup_version="0.0.1"> <sequence>

4 <module name="Magento_Catalog"/> </sequence>

5 </module>

6 </config>

Notice that in XML file we identified:

  • Module name: Learning_FirstUnit (according to the folders we generated)
  • Version: 0.01 (initial version of our module)
  • Dependency: Magento_Catalog. We could have many dependencies. In this case, we would enter <module name =”. .” / > nodes under the sequence node.

Step 3: Generate the registration.php file

Every module must have this file, which shows Magento how to place the module. Continuing our example, build the file app / code / Learning / FirstUnit / registration.php. Then apply the content below to it:

1 <?php \Magento\Framework\Component\ComponentRegistrar::register(

2 \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',

3 __DIR__

4 );

The registration.php is a standardized file that follows the same pattern for all modules. The only thing that is variable is the module name, which is Learning_FirstUnit in this case.

Step 4: Run the “setup:upgrade” command

Applying this command activates your new module, informing Magento of its presence.

$ php bin/magento setup:upgrade

Step 5: Ensure that the new module is activated

We haven’t included any helpful code to our module - it is still empty. To identify that it has been recognized, check the file app / etc / config.php. It contains a list of auto-created modules that are activated.

Never modify this list manually!

$ grep Learning_FirstUnit app/etc/config.php

Final words

That’s all the guide to building an extension package file in Magento. Follow these steps carefully, and then you can do it by yourself. In case you still don’t understand any part in the instructions, drop a line in the comment section. We’re pleased to help you.

Looking for
Customization & Development Services?

8+ years of experiences in e-commerce & Magento has prepared us for any challenges, so that we can lead you to your success.

Get free consultant
development service
x

    Explore Our Products:

    Subscribe

    Stay in the know

    Get special offers on the latest news from Mageplaza.

    Earn $10 in reward now!

    Earn $10 in reward now!

    comment
    iphone
    go up