Learn how to create and register a Magento module.
- [Instructor] Module is a structural element of Magento 2. The whole system is built upon modules. Typically, the first step in creation and customization of any feature is to create a module. To create a module, we'll need to perform below action item. First thing is, we'll need to create a module folder. Earlier we checked that in vendor Magento we have module folders. After that, we'll have to create a registration.php file. As we saw in module catalog. And a module.xml file.
These two files tell Magento that there is a module it needs to register and to invoke. Now to create our first module, we'll go to app directory. Here we'll be creating a folder named as code. In Magento 2, modules can reside in two places. Either in app called folder or under vendor folder. Generally if you want to ship your module through Composer, you can create the module in vendor folder.
Or you can create your module in app code folder if they are specific to a local project. Inside your code folder, you'll have to create a new folder named with your vendor name. For this course, I'll use Hplusport as my vendor name. Inside your vendor name, I'll create a module named as Chapter2. So inside my app, I'll have a code folder where all my modules will reside.
Inside code folder, you'll have your vendor name. And inside your vendor name, you'll have your module's name. In your modules folder, you'll have to create two files. First is registration.php. The second one is module.xml. To create resgistration.php, you can use the same code from your module catalog. Just paste it here. Make sure you change your module's name in here, and if you have any specific changes, you can also perform in terms of commenting here.
For me it will be. The second file that we need to create is module.xml. Module.xml will reside inside etc folder. So, I'll create a new folder named as etc. Inside etc folder, I'll create my module.xml file.
This module.xml will tell Magento about my module's version. I can just copy-paste this. Musically in this file, we are using Magento's etc module.xsd schema. And in that we are going to specify our module's version. This will be 1.0.0. And my module's name is Hplusport underscore Chapter2. The sequence section will tell Magento that what modules it needs to load, prior loading this module.
For this case, we'll have no sequence. So I can just delete that and save. After that, I have to go to my Terminal. I can run php bin slash magento setup colon upgrade. This command will install these modules into Mageno. This may take some time. Once this is completed, you can go to your app directory.
Inside app you will see etc folder and then config.php. Inside config.php, you shall see your module registered here. If you see this line here, your module is installed and is successfully working.
- Setting up your development environment
- Installing and upgrading the schema
- Creating modules
- Magento ORM
- Creating models and collections
- Customizing Magento
- Defining plugins