How to Create Module in Magento 2
We will cover how to create a Hello World module in Magento 2, providing you with the clearest and simplest approach to get started. Keep in mind that the concepts of local, community, and core folders, which were present in Magento 1, are no longer applicable in Magento 2.
To create Hello World module in Magento 2.
To create Hello World module, you need to complete the following high-level steps:
- Step 1: Create the folder for the Hello World module
- Step 2: Create etc/module.xml file
- Step 3: Create etc/registration.php file
- Step 4: Enable the module
Step 1: Create the folder of Hello World module
The name of the module is defined as VendorName_ModuleName. The first part represents the vendor name, while the last part indicates the module name. For example: Magento_HelloWorld, Mavenbird_Pdf_Invoice, Mavenbird_One_Step_Checkout. Follow the guide below to create the necessary folders:
app/code/Mavenbird/HelloWorld
Step 2: Create etc/module.xml file.
Then, it is necessary to create etc folder and add the module.xml file
app/code/Mavenbird/HelloWorld/etc/module.xml
Contents would be:
Step 3: Create etc/registration.php file
In this step, we will add registration.php as following guide:
app/code/Mavenbird/HelloWorld/registration.php
Contents would be:
\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Mavenbird_HelloWorld', __DIR__ );
Step 4: Enable the module
Now that we have created the HelloWorld module, let's proceed to finish Step 3 by enabling this module.
After creating the module, you can run the following command:
php bin/magento module:status
You should see that the module is currently disabled:
List of disabled modules: Mavenbird_HelloWorld
Follow the guide below to enable the module. Run the following command:
php bin/magento module:enable Mavenbird_HelloWorld
Or other way, you can access the file:
app/etc/config.php
You will see a long list of modules there, just add your module as well
...
'Mavenbird_HelloWorld' => 1,
....
Your module should be available now.
After this step, when you open your website in the browser, you may encounter an error message stating:
Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory.
Now, let's run the following command:
php bin/magento setup:upgrade
After complete,when you open your website in browser you will see the layout of the website is broken.
Please run
php bin/magento setup:static-content:deploy
to fix this.
After the deployment is complete, you can view your module from the backend at System Configuration -> Advanced -> Disable Modules Output.
Next, we will create a controller to test the module.
Before creating a controller, we need to establish a route for the HelloWorld module.
Routes in Magento are divided into three parts: the route front name, the controller, and the action, as shown in the following example:
http://mavenbird.com/index.php/frontname/controller/action
To add a route, you need to create a routes.xml file at the following location:
app/code/Mavenbird/HelloWorld/etc/frontend/routes.xml
Since this is a frontend route, we place it in the frontend folder; otherwise, it should be added to the adminhtml folder.
The content of the routes.xml file would be:
After define the first part of the route, the URL will be displayed as:
http://domain.com/helloworld/*
Next, we will continue by creating the controller and action.
The folder and file you need to create are:
app/code/Mavenbird/HelloWorld/Controller/Index/Test.php
The contents of this file would be:
namespace Mavenbird\HelloWorld\Controller\Index; class Test extends \Magento\Framework\App\Action\Action { protected $_pageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory) { $this->_pageFactory = $pageFactory; return parent::__construct($context); } public function execute() { echo "Hello World"; exit; } }
After completing the steps, please run the following command to clear the cache:
php bin/magento cache:clean
Now, your URL should be:
http://domain.com/helloworld/index/test
After finish all steps, the output Hello World should be displayed in your browser when you open the URL. We hope our guide is very useful and effective for you.