A Complete Tutorial on Magento 2 Module Development
“Hello World” is where a new programmer is born! 😄
Starting with the ABCD of Magento 2 development, hello world module is mandatory!
So, here I am, with a post for newbies to create a simple module in Magento 2, for hello world. Magento 2 module development is a stepwise process and needs to be done with some prerequisites conditions.
Follow the below tutorial and create your own basic module in Magento 2!
Mục Lục
Things to take care of before you create a custom module in Magento 2:
Switch to developer mode: Switch to developer mode in order to see every error Magento is throwing at you. Use the below command:
1
php
bin
/
magento
deploy
:
mode
:
set
developer
Learn more about the Magento 2 Modes
Method for Magento 2 Module Development:
Getting to each step,
-
Module Setup
- Create the below folders
- app/code/Meetanshi
- app/code/Meetanshi/Helloworldwhere Meetanshi folder is the module’s namespace and our module’s name is Helloworld!Note: You’d be required to create the “code” folder manually if it is not in the “app” directory.
- Create a module.xml file in the app/code/Meetanshi/Helloworld/etc folder with the below code:
1
2
3
4
5
6
<?
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
=
“Meetanshi_Helloworld”
setup_version
=
“1.0.0”
>
<
/
module
>
<
/
config
>
- Create a registration/php file in the app/code/Meetanshi/Helloworld folder to register the module. Implement the below code:
1
2
3
4
5
6
7
<
?
php
\
Magento
\
Framework
\
Component
\
ComponentRegistrar
:
:
register
(
\
Magento
\
Framework
\
Component
\
ComponentRegistrar
:
:
MODULE
,
‘Meetanshi_Helloworld’
,
__DIR__
)
;
- Open the terminal and go to the Magento 2 root. Run the below command
1
php
bin
/
magento
setup
:
upgrade
Note: Make sure if your module is installed. Go to Admin > Stores > Configuration > Advanced > Advanced. Check if the module is present in the list. Else, open app/etc/config.php and check the array for the “Meetanshi_Helloworld” key whose value must be 1.
- Create the below folders
-
Creating a Controller
- Define the router with routes.xml file in the app/code/Meetanshi/Helloworld/etc/frontend folder with the below code:
1
2
3
4
5
6
7
8
9
<?
xml
version
=
“1.0”
?>
<
config
xmlns
:
xsi
=
“http://www.w3.org/2001/XMLSchema-instance”
xsi
:
noNamespaceSchemaLocation
=
“urn:magento:framework:App/etc/routes.xsd”
>
<
router
id
=
“standard”
>
<
route
id
=
“helloworld”
frontName
=
“helloworld”
>
<
module
name
=
“Meetanshi_Helloworld”
/
>
<
/
route
>
<
/
router
>
<
/
config
>
- Create the Index.php file in the app/code/Meetanshi/Helloworld/Controller/Index folder with the below code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<
?
php
namespace
Meetanshi
\
Helloworld
\
Controller
\
Index
;
use
Magento
\
Framework
\
App
\
Action
\
Context
;
use
Magento
\
Framework
\
App
\
Action
\
Action
;
use
Magento
\
Framework
\
View
\
Result
\
PageFactory
;
class
Index
extends
Action
{
protected
$
_resultPageFactory
;
public
function
__construct
(
Context
$
context
,
PageFactory
$
resultPageFactory
)
{
$
this
–
>
_resultPageFactory
=
$
resultPageFactory
;
parent
:
:
__construct
(
$
context
)
;
}
public
function
execute
(
)
{
$
resultPage
=
$
this
–
>
_resultPageFactory
–
>
create
(
)
;
return
$
resultPage
;
}
}
In Magento 2 every action has its own class which implements the execute() method.
- Define the router with routes.xml file in the app/code/Meetanshi/Helloworld/etc/frontend folder with the below code:
-
Creating a block
Create a simple block class with the getHelloWorldTxt() method which returns the “Hello world” string.
- Create a Helloworld.php file in the app/code/Meetanshi/Helloworld/Block folder with the below code:
1
2
3
4
5
6
7
8
9
10
11
12
<
?
php
namespace
Meetanshi
\
Helloworld
\
Block
;
use
Magento
\
Framework
\
View
\
Element
\
Template
;
class
Helloworld
extends
Template
{
public
function
getHelloWorldText
(
)
{
return
‘Hello world!’
;
}
}
- Create a Helloworld.php file in the app/code/Meetanshi/Helloworld/Block folder with the below code:
-
Creating a layout and template files
Layout files and templates are placed in the view folder inside the module. We can have three subfolders inside the view folder: adminhtml, base, and frontend.
- Use the below code to create a helloworld_index_index.xml file in the app/code/Meetanshi/Helloworld/view/frontend/layout folder:
1
2
3
4
5
6
7
<
page
xmlns
:
xsi
=
“http://www.w3.org/2001/XMLSchema-instance”
xsi
:
noNamespaceSchemaLocation
=
“../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd”
layout
=
“1column”
>
<
body
>
<
referenceContainer
name
=
“content”
>
<
block
class
=
“Meetanshi\Helloworld\Block\Helloworld”
name
=
“helloworld”
template
=
“helloworld.phtml”
/
>
<
/
referenceContainer
>
<
/
body
>
<
/
page
>
Every page has a layout hand. For our controller action, the layout handle is helloworld_index_index. You may create a layout configuration file for every layout handle. In our layout file, we have added a block to the content container and set the template of our block to helloworld.phtml, which we create in the next step.
- Create a helloworld.phtml file in the app/code/Meetanshi/Helloworld/view/frontend/templates folder
1
2
<
h1
>
<?php
echo
$this
–
>
getHelloWorldText
(
)
;
?>
<
/
h1
>
<
!
—
Display
Text
from
block
file
—
>
$this variable refers to our block class. Our method getHelloWorldTxt() returns the string “Hello world!”
- Use the below code to create a helloworld_index_index.xml file in the app/code/Meetanshi/Helloworld/view/frontend/layout folder:
Open the /helloworld/index/index URL in the browser to check the below output.
That’s it with the custom Magento 2 module development!
Hopefully, the post helps you go through the first Magento 2 module development stage! If you get stuck in the between, make sure to solve your doubts using the Comments section below. I’d be happy to help 🙂
Do rate the post with 5 stars!
Thank you.
4.9
(based on
28
Reviews)