Creating a new module in Magento2
We can place modules inside the app/code folder and vendor folder, If we are creating a module for any customization or specific project we will create inside the app/code folder. Whenever you install a module using composer it is added to the vendor folder.
These are the required files to create a new module:-
registration.php
module.xml
Register your module:- Create registration.php inside Vendor/Module folder
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Custom_Module',
__DIR__
);
\Magento\Framework\Component\ComponentRegistrar class is used to register the module.
module.xml :- Create this file inside etc/module.xml and This file is used to declare name, dependencies and version of the module.
<?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="Custom_Module" setup_version="0.0.1">
</module>
</config>
Module version is used to check the current version of database schema and data and it is used for upgrading. For example we have added a new column in a database table and we need to make sure that the column is added to the table for that we need to update the version here and then run setup upgrade command.
We can define the dependencies of a module using a sequence tag in module.xml. This tag tells us component load order, those components that are mentioned in a sequence tag will be loaded before the current component.
<sequence>
<module name="Magento_Catalog"/> </sequence>
This defines that Magento_Catalog will load before the current component.
Comments
Post a Comment