New module in Magento 2 - Tutorial - Paul Meștereagă
Magento 2

Create a new module in Magento 2

Now that we’ve seen in the previous post how to install Magento 2 on localhost I continue the series with a short tutorial about how to create a new module in Magento 2. Module Location The folder in which the modules are stored in Magento 2 is app/code. The full folder structure of a module…

author

Paul Mestereaga

November 6, 2017

Now that we’ve seen in the previous post how to install Magento 2 on localhost I continue the series with a short tutorial about how to create a new module in Magento 2.

Module Location

The folder in which the modules are stored in Magento 2 is app/code. The full folder structure of a module would be app/code/Namespace/ModuleName. So the main folder of a module represents the namespace and the second the module name. For example we create the folders Mesteru and FirstModule like this:

app/code/Mesteru/FirstModule

Register a new module

In the root folder of our module we create the file registration.php where we register our module within Magento 2. The content of the file is:

<?php 
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Mesteru_FirstModule',
__DIR__
);

The configuration files are placed into etc folder. General configuration file is module.xml The content of etc/module.xml is:

<!--?xml version="1.0"?-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Mesteru_FirstModule" setup_version="0.0.1"> 
		<sequence>
			<module name="Magento_Catalog"> 
		</module></sequence>
    </module>
</config>

The <module> node declares the module with it’s name and version. The  <sequence> node declares the dependencies. Our module is loaded after the modules declared here thus they can be rewritten by our module.

Enable the module

To enable our module run the following command in the console:

php bin/magento module:enable Mesteru_FirstModule

Observe app/etc/config.php. There you can see our module.

Also run the following command that will apply the database changes:

php bin/magento setup:upgrade

New module in Magento 2

Now we have a new module declared in Magento 2. The module is empty but in the following posts we’ll go further and add logic to it.

Get the code

You can get the complete code on my github profile here.

Magento 2 from Zero to Beginner Free Online Course

In 10 hours you will have all the basic knowledge so that you could develop your modules without wandering around how to implement stuff.

Related Posts

3 Things That Kill Your Magento Store’s Conversion Rate

10 Dec 2021

|

Paul Mestereaga

The store receives a lot of traffic, and you market to the right audience. The brand is actively promoted on social media and succeeds in SEO, so the marketing efforts appear to be working.
But what if it doesn’t lead to many sales?

How to create a product attribute using Data Patches in Magento 2.3

26 Mar 2020

|

Paul Mestereaga

This is a short tutorial in which we will learn to create a product attribute using Data Patches in Magento 2.3

optimize-magento-preview

FREE!

Magento 2
Optimization guide

Wondered how you can make your Magento load faster? Wonder no more. Here is a step by step guide that helps you.