Create new module

  

  

To create a module, you have to complete the following steps:Create the model foldercreate the "module dot xml" fileCreate the registration dot php fileRun setup:upgrade script to install the new module and check the module is working.So let's do itEach module name in Magento 2 consists of two parts.The vendor name and the module name. Modules are grouped into vendors, so you need to define the vendor and module names.Let's create something like TestVendorName/TestModuleName.And then you have to create "e t c /module dot xml" file.This file is required for the module to exist.We can get an example of this file in some other module.Let's say magento catalog.By default, an IDE such as PHPStorm is not configured to highlight XML URNs.As a result, they are displayed in red text as follows. To fix this, we can use a special command from Magento.Let's try it.We need to use the following command "dev:urn-catalog:generate". This command allows your IDE, now PHPStorm only, to recognize and highlight the Magento URN as shown.We have to specify .idea in this fileAnd now we need to restart our PHPStorm to see the changes.As you can see, this command creates the following PHPStorm configuration.Now we can use the Magento URN and specify, for example, the installation version.Ok, let's take a look at the Module dot xml file.This file contains the following information: the module name, the module version, and dependencies. In Magento, class names must follow the folder structure because we created the Folders TestVendorNameTestModuleNameOur module name will be TestVendorName_TestModuleName, so let's change it.And all classes that belong to this module, we'll begin with the TestVendorName_TestModuleName.Let's look at some Magento module.As you can see, all classes start with Magento\Catalog because the name of this module is Magento_Catalog.The module version specifies the current version of the database schema and data and is used when upgrading.We will discuss this in detail as part of the database workflow.But keep in mind that this is an outdated approach.So now we can use Schema dot xml files and patches instead of update scripts.But for testing purposes we can leave everything as is.Dependencies.If one module depends on another, there will be a special declaration about this in the Module dot xml file.Let's say we have a dependency on Magento module. For example Magento_Catalog.As a result, we cannot disable the Magento_Catalog module if we need to use TestModuleName. Magento will throw an error and will not allow us to do this.And now we need to create a Registration PHP file because every module needs to have a file that tells Magento how to find the module.And again, we can use the example from the Core Magento code.We have to change the module name.And now we can execute bin/magento setup:upgrade command.Let's check app/etc/config.php fileHere it is - TestVendorName/TestModuleName.We can also check the list of modules on the command line.Here it is.In addition, we can enable or disable it. Let's do it.This module is currently disabled.If we need to enable this module, we can do it via the command line.If you need to see all possible commands for Magento. Just enter bin/magentoLooks good, we've added a new module!
You have completed 0% of the lesson
0%