Now we can create new a module, so that we can get some idea about magento2.
Namespace: Jute
Module Name is: Sample
Declare our sample module first.
Step1: we need create a module.xml file in app/code/Jute/Sample/etc/module.xml (By default magento2 don’t have any folder called code. we have to create)
Ex: File Path: app/code/Jute/Sample/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=”Jute_Sample” setup_version=”1.0.0″ schema_version=”1.0.0″>
</module>
</config>
Name tag: Here we have to enter out Namespace_ModuleName
Version: This is same as like magento1.x. We will declare our version here. In case once installed the module after that if you change these version then you have to run following command in command prompt from magento root path
Magento root> Php magento2\bin\magento setup:upgrade (Windows)
Magento root> Php magento2/bin/magento setup:upgrade (Ubuntu)
Step2: Create registration.php file. (Register your module or component by creating these file)
File Path: app/code/Jute/Sample/registration.xml
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Jute_Sample’,
__DIR__
);
Step3: Create a frontend router. In case if it is admin purpose then you have to create adminhtml/routes.xml
File Path: app/code/Jute/Sample/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”> <!—Frontend controller or admin controller –>
<route id=”sample” frontName=”sample”> <!—block name–>
<module name=”Jute_Sample” /> <!—Namespace_Modulename–>
</route>
</router>
</config>
Below code is common for all the module routes files.
The first section of the route string indicates which node Magento should look at to find the URL’s front Name.
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd”>
Then, the router ID showed which router we will use: frontend or adminhtml (the same like in Magento1). Attention that the front name is the first part of the URL and it should be unique.
Step4: Create a Controller action
Create the file index.php from following path app/code/Jute/Sample/Controller/Index. This will map to http://localhost/magento2/sample/index/index
File Path: app/code/Jute/Sample/Controller/Index/Index.php
<?php
namespace Jute\Sample\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {
public function execute() {
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}
Each action is its own class extending \Magento\Framework\App\Action\Action. In every action file, there will be a method name execute () that will invoked when the action is called.
Step5: Let’s create a block for our module.
File Path: app/code/Jute/Sample/Block/Sample.php
<?php
namespace Jute\Sample\Block;
class Sample extends \Magento\Framework\View\Element\Template {
public function _prepareLayout() {
return parent::_prepareLayout();
}
}
Step6: Create a layout file in the following directory
File Path: app\code\Jute\Index\view\frontend\layout\sample_index_index.xml
<?xml version=”1.0″?>
<page xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” layout=”1column” xsi:noNamespaceSchemaLocation=”../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd”>
<body>
<referenceContainer name=”content”>
<block class=”Jute\Sample\Block\Sample” name=”sample” template=”sample.phtml”>
</block>
</referenceContainer>
</body>
</page>
Step7: Create a template file for frontend purpose.
File Path: app/code/Jute/Sample/view/frontend/templates/sample.phtml
<h1 style=”color:#f1703d”> Welcome to Magento 2 – Mr.Jute </h2>
Step8: Activate Jute_Sample extension or module or component.
We have two ways to activate Jute_Sample module.
1. Directly edit file app/etc/config.xml: In the array module, add the element: ‘Jute_Sample’ => 1
2. Run below command. Once you run the below command your module will be enable. You just open config.xml file, your module will activate with status 1.
Magento root> Php magento2\bin\magento setup:upgrade (Windows)
Magento root> Php magento2/bin/magento setup:upgrade (Ubuntu)
3. Using below command we can check out module is enabled or not.
php bin/magento module:status
It will show List of enabled modules (OR)
Finally check it from Stores -> Configuration -> Advanced -> Advanced -> Here your module will enabled status.
Step9: Yes we are almost we are ready to check our module. We have to trigger following URL from browser.
http://localhost/magento2/sample/index/index
Step10: we will get the output like
O/P: Welcome to Magento 2 – Mr.Jute with Luma theme & design