I am going to create simple custom module in magento2. This module will be used magento’s block, layout, template and router section. I will be explaining these section one by one but not briefly. But you will be getting the basic understanding on magento’s module with creating simple custom module in magento2 with router as well as accessing template file from block or layout.If you are a magento1 developer then there is no issue to understand this tutorial, because this most of all logic of magento1 is similar to magento2 . But if you are not then you must have knowledge on MVC.
So before starting any developement we need to check two things in magento2.
1: Disable magento2’s cache
navigate : backend => system => cache management => select all cahce type => disable them
2: set magento mode into developer mode
In developer mode you will be getting the benefit of getting all errors that magento through.
command: php bin/magento deploy:mode:set developer
Magento1 has code pool concept but in magento2 that has been removed. Here modules are grouped by namespaces and placed directly in app/code folder.
Module creations step:
All modules names divided into two parts 1: namespace 2: actual module name. Let take example of creating our module “JRB_HelloWorld“. Here we will divide our module “JRB_HelloWorld” into two parts
- namespace: JRB
- actual module name: HelloWorld
so depending upon that we will create folder like app/code/JRB/HelloWorld and all folder belongs to this module will be placed inside this folder app/code/JRB/HelloWorld.
We need to declare our module by module.xml file. So lets create and declare inside app/code/JRB/HelloWorld/etc/module.xml
Include our module into magento2’s autoload class by registration.php file. So lets create registration.php inside the folder app/code/JRB/HelloWorld/
Now empty module has been created and if you run the command: php bin/magento module:status.
you could see list disable modules. From those list our module will be also there. So for enabling our module just run the command: php bin/magento module: JRB_HelloWorld
after doing all these process, you need to run php bin/magento setup:upgrade
after upgrading you will be getting our custom module in backend with enable status. Navigate to see : admin => store => configuration => advanced => disable module output
First we need to add router. To do this we need to create routes.xml file in the folder app/code/JRB/HelloWorld/etc/frontend (frontend : for frontend , adminhtml : for admi side controller)
here we saw frontend router with router id helloworld. So as similar to magento1, magento2 has the frontName as first part of URL.
so for our example final URL will be looking like this: http://example.com/helloworld/index/index/
- helloworld: frontName
- index: controller
- index: controller class
Now we need to create Index.php controller file having below code.[crayon-5e44fd29f027f208664509/]
In magento1 each controller has multiple action but in magento2 each action has its own class which implements execute methods.
Now we created a simple module in magento which has just contained router and for block we need to go for step6;
we will create simple block class with getHelloWorldTxt method which will return “My first modules with “hello world” .
create a Helloworld.php class inside the folder app/code/JRB/HelloWorld/Block/
In step7 we will create layout and template to display our out put to user. So layout files and template are placed in view folder of each module. Inside view folder we can have three sub folder
- adminhtml (for admin)
- base (for both frontend and admin)
- frontend (for frontend)
first we will create helloworld_index_index.xml file inside the folder app/code/JRB/HelloWorld/view/frontend/layout/ with following code[crayon-5e44fd29f0287083326971/]
Each layout has a handle(“helloworld_index_index“) . We have added block to the content container and set the template helloworld.phtml .
create a helloworld.phtml file inside app/code/JRB/HelloWorld/View/Frontend/templates with following code.[crayon-5e44fd29f028b319030144/]
download full module from herebest sellers in amazon