Basic requirments
First , we need to download latest magento 2 version.
please disable cache before we start module development in magento 2.
Creating Simple module
These are the one by one steps for develop Helloworld module.
1) Create module namespace folder in app/code/namespacefoldername
for example : app/code/Mageniks
Mageniks is my module namespace.
2) Create module name folder under the namespace folder.
for example : app/code/Mageniks/Alltest
so finally 1) Mageniks is our namespace.
2) Alltest which is module folder name under the Mageniks.
3) Now create first registration.php file for register module.
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mageniks_Alltest',
__DIR__
);
4) Create etc folder in module and then Create module.xml file in etc folder.
<?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="Mageniks_Alltest" setup_version="2.0.0">
</module>
</config>
In this xml, we define our module name Mageniks_Alltest and declare module version 2.0.0.These are basic files registration.php and module.xml which is use for creating module.
After creating these file, you want to run upgrade command in terminal.
php bin/magento setup:upgrade
run this command in terminal and you can see the module name in registerd list modules.
5) Now, create frontend folder in etc. and then create routes.xml file for routing module in etc/frontend folder.
In routes.xml, add following code.
<?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="alltest" frontName="alltest">
<module name="Mageniks_Alltest" />
</route>
</router>
</config>
Explain above xml code below :<router id="standard"> : router id standard which decalre that this module routing is for frontend.
<route id="alltest" frontName="alltest">
<module name="Mageniks_Alltest" />
</route>
This code in declare frontname alltest for call module in frontend.
6) Create Controller folder. In Controller folder we need to create Index controller and then create action file in Index controller folder.
In magento 2 we have create Controller folder for define Controller and under the controller folder we can create number of action files.
And each action files in create execute() function for exceute action code.
For example:
I have create Index controller in Controller/Index and then create Index.php in Controller/Index/Index.php
so Index is my Controller and Index.php is my action of Index Controller.
In Index.php we have execute function for execute our action. please see below action code.
<?php
namespace Mageniks\Alltest\Controller\Index;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
Explain above action code below :namespace Mageniks\Alltest\Controller\Index : namespace define location of our controller and action.
In execute function we have render and load layout function which is also available in magento 1.x, In magento 2 just change the calling syntax.
This load and reader layout call our xml file alltest_index_index.xml and load template output.
7) After create action in controller, call template file using xml file.
for create xml file, you need create view/frontend/layout folders and under the layout folder, you want to create alltest_index_index.xml.
Please see code of alltest_index_index.xml.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<title>All Test</title>
<script src="Mageniks_Alltest::js/demo.js"/>
</head>
<body>
<referenceBlock name="content">
<block class="Mageniks\Alltest\Block\Alltest" name="alltest" template="Mageniks_Alltest::alltest.phtml"/>
</referenceBlock>
</body>
</page>
Explain above xml code below :Magento first call block file "Mageniks\Alltest\Block\Alltest" and then call template file template="Mageniks_Alltest::alltest.phtml".
So create block file Alltest.php in Block folder and then create alltest.phtml in templates folder.
8) Create Alltest.php in Block folder.
<?php
namespace Mageniks\Alltest\Block;
class Alltest extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}
}
9) After creating block, we have to create templates folder in view/frontend. In templates folder, we need to create alltest.phtml file.written hello world in alltest.phtml.
<?php echo "hello world"; ?>
10) Now just activate the module by running the below commands in your Magento’s root directory.
11) Now run module in browser http://localhost/magento21/alltest/index/index and display hello world output in front-end.
when you sent request http://localhost/magento2/alltest/index/index in browser, system search frontname alltest and then find index controller in index action.
After find action, system execute index action code and call xml file alltest_index_index.xml.
In xml file call block file and then call templete file from xml.
after called templete file we see the output in front-end.
These is whole process of our module.
please see the screen shot of output.
1. If you want to check the status of your module then just fire below command
php bin/magento module:status
2. If module is in the disabled then just run below command to enable it. If module is already enabled then follow the next step.
php bin/magento module:enable Mageniks_Alltest
3. Now upgrade the setup:
php bin/magento setup:upgrade
4. clean the cache:
php bin/magento cache:clean
php bin/magento cache:flush
That's it. Now you can check your module's output in front-end.
11) Now run module in browser http://localhost/magento21/alltest/index/index and display hello world output in front-end.
when you sent request http://localhost/magento2/alltest/index/index in browser, system search frontname alltest and then find index controller in index action.
After find action, system execute index action code and call xml file alltest_index_index.xml.
In xml file call block file and then call templete file from xml.
after called templete file we see the output in front-end.
These is whole process of our module.
please see the screen shot of output.