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…

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.

Related Posts

Extend Magento 2 jQuery Widget

18 Jun 2019

|

Paul Mestereaga

In this short tutorial we will learn how to extend Magento 2 jQuery widget with our custom logic.

Console Commands in Magento 2

21 May 2019

|

Paul Mestereaga

In this tutorial I will give an overview of the console commands in Magento 2 and we will learn how to build a custom console command in Magento 2

Optimize Magento 2

Free guide to help you optimise your Magento 2 installation and improve your website speed

Get your free optimisation guide.

Enter your name and email to join my mailing list in exchange for the free download. You can opt out any time.


Can I email you?

I will use the information you provide on this form to stay in touch, send you updates, and send marketing emails about our products and services. You can unsubscribe at any time through a link in the footer of an email from me. For more information, please read the Privacy Policy.