A useful tutorial I found on creating a custom module in Magento 2 yourself.
Create Directories
Magenticians: Namespace of Module
Mymodule: Name of module.
Block: Contain PHP files
Controller: Controls the flow of application execution.
Index: Module controller name and it contains the action file.
Etc: Configuration file of module will be placed here.
Etc/Frontend: Router file will be placed here.
View/Frontend: Layout and Templates Folder will be created here.
View/Frontend/Layout: Contain XML file.
View/Frontend/Templates: Contain .phtml file.
Configuration of Module
Now configure your module. Create module.xml file in app/code/Magenticians/Mymodule/etc and put the following code in this file:
<?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="Magenticians_Mymodule" setup_version="2.0.0" />
</config>
Registration of Module
Registration of Module is required. To do this, go to app/code/Magenticians/Mymodule and create registration.php. Put the following code in it:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magenticians_Mymodule',
__DIR__
);
?>
Front End Router File
The front name in the router file will be used to access the module through the URL. Create routes.xml in app/code/Magenticians/Mymodule/etc/frontend. Put the following code in it:
<?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="mymodule" frontName="mymodule">
<module name="Magenticians_Mymodule" />
</route>
</router>
</config>
Create Controller
To create controller class for each action, you have to create separate files for each. Now we will create Index.php file in app/code/Magenticians/Mymodule/Controller/Index. Put the following code in it:
<?php
namespace Magenticians\Mymodule\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Default customer account page
*
* @return void
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
?>
Create Block
As the block contains PHP view classes, so now we will create Mymodule.php file in app/code/Magenticians/Mymodule/Block. Put the following code in it:
<?php
namespace Magenticians\Mymodule\Block;
class Mymodule extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = []
)
{
parent::__construct($context, $data);
}
public function getMymodule()
{
return 'Module Cretaed Successfully';
}
}
?>
Frontend layout File
Create mymodule_index_index.xml in app/code/Magenticians/Mymodule/view/frontend/layout. This (mymodule_index_index.xml) file is a frontend layout file in which I will write the title for this page and give the template file name in the Block class. Put the following code in it:
<?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>
Magento 2 Module
</title>
</head>
<body>
<referenceContainer name="content">
<block class="Magenticians\Mymodule\Block\Mymodule" name="mymodule" template="Magenticians_Mymodule::mymodule.phtml" />
</referenceContainer>
</body>
</page>
Frontend Template File
Now create a template file which will be used to call block function. Create mymodule.phtml in app/code/Magenticians/Mymodule/view/frontend/templates. Put the following code in it:
<h2>
<?php echo $block->getMymodule(); ?>
</h2>
Module Activation
All done! Now just activate the module by running the commands in your Magento’s root directory:
1- Check the status of your module:
php bin/magento module:status
2- If your module is in the disabled list then just run this command to enable it otherwise move to step 3 :
php bin/magento module:enable Magenticians_Mymodule
3- Now upgrade the setup:
php bin/magento setup:upgrade
4- At last, clean the cache:
php bin/magento cache:clean
php bin/magento cache:flush
Launch the browser, enter your store URL , and add /mymodule or /mymodule/index/index at the end of URL.
Source: Create module magento 2