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

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

View Layer in Magento 2

7 Jun 2018

|

Paul Mestereaga

In this tutorial I will give an overview of the Magento 2 View Layer. We will learn how to build Blocks, Templates and Layouts.