Create Magento 2 hello world module in 2 minutes!
To create a simple custom module in Magento2 which will give an output on frontend website from your module, follow below steps:
Change to app/code directory where all the Magento2 module lives. Notice there are no codepools (core, community, local) under this directory like it used to be in Magento 1.x
Create your company name directory
Create you module name directory
Create etc directory to hold module config file
module.xml file is required only to hold module name and it’s version
| vi Hello/World/etc/module.xml
|
| <?xml version="1.0"?>
|
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
|
| <module name="Hello_World" setup_version="0.1.0" />
|
| </config>
|
Register your module, this is required in all the modules. Only thing you will change is your module name (Hello_World)
| vi Hello/World/registration.php
|
| <?php
|
| \Magento\Framework\Component\ComponentRegistrar::register(
|
| \Magento\Framework\Component\ComponentRegistrar::MODULE,
|
| 'Hello_World',
|
| __DIR__
|
| );
|
Create frontend directory under etc to define frontend routes. We are creating this because we want to output something on frontend website.
| mkdir Hello/World/etc/frontend
|
routes.xml is used to give your module a front name. Only thing you will be interested is tag which holds frontName for the enclosed module name.
| vi Hello/World/etc/frontend/routes.xml
|
| <?xml version="1.0"?>
|
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
|
| <router id="standard">
|
| <route id="first" frontName="first">
|
| <module name="Hello_World" />
|
| </route>
|
| </router>
|
| </config>
|
Create Controller directory to hold your module’s controller files
| mkdir Hello/World/Controller
|
Create your controller directory, this will become part of the url after frontName
| mkdir Hello/World/Controller/Hello
|
Create controller file, notice there is no controller action in Magento2 Controller. This will again become part of the url and will call execute() method of the class.
| vi Hello/World/Controller/Hello/World.php
|
| <?php
|
| namespace Hello\World\Controller\Hello;
|
| class World extends \Magento\Framework\App\Action\Action
|
| {
|
| public function execute()
|
| {
|
| echo 'Hello world!';
|
| }
|
| }
|
Change directory to reach Magento 2 root directory
Enable your module, so an entry will go to app/etc/config.php file and cache will be cleared
| bin/magento module:enable Hello_World
|
This will add your module with it’s current version to DB table setup_module, without running this command you won’t see the changes of newly created module.
| bin/magento setup:upgrade
|
Check your module in browser: http://www.yourwebsite.com/first/hello/world. If you have done everything correct, it should output “Hello world!”
