In this blog, I'll guide you through the process of creating a Block, Controller, Template file, and their corresponding layout files.
I hope you must be a developer reading this blog so i would give you tips how you can improve your efficacy.
If you are using a PHPSTORM then you can use live template that will make your work much smoother and less work hasle.
Create A Controller:
<?php
namespace Anees\Training\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Index extends Action
{
/**
* @var PageFactory
*/
protected $pageFactory;
/**
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(Context $context, PageFactory $pageFactory)
{
parent::__construct($context);
$this->pageFactory = $pageFactory;
}
/**
* Index Action
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->pageFactory->create();
return $resultPage;
}
}
Create Controller: Inside your module directory (app/code/Anees/Training/Controller/
), create your controller file, say Index.php
. For example:
<?php
namespace Anees\Training\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Index extends Action
{
/**
* @var PageFactory
*/
protected $pageFactory;
/**
* @param Context $context
* @param PageFactory $pageFactory
*/
public function __construct(Context $context, PageFactory $pageFactory)
{
parent::__construct($context);
$this->pageFactory = $pageFactory;
}
/**
* Index Action
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->pageFactory->create();
return $resultPage;
}
}
Create routes.xml: In the etc
directory of your module (app/code/Anees/Training/etc/
), create routes.xml
file if it doesn't exist, and define your routes.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard"> <!-- standard or admin -->
<route id="training" frontName="training">
<module name="Anees_Training" />
</route>
</router>
</config>
Create Block: Create your block class inside Block
directory of your module (app/code/Anees/Training/Block
).
<?php
namespace Anees\Training\Block;;
use Magento\Framework\View\Element\Template;
class Index extends Template
{
/**
* Constructor
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
parent::__construct($context, $data);
}
/**
* Example method in custom block
*
* @return string
*/
public function getCustomMessage()
{
return __('This is a custom message from your block.');
}
}
Linking Block with PHTML: If you need to use the block in the PHTML file, you can do so in the layout XML file (app/code/Anees/Training/view/frontend/layout/your_layout.xml
)
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<!-- Add your head elements here -->
</head>
<body>
<referenceContainer name="content">
<!-- Add your content blocks here -->
<block class="Anees\Training\Block\Index" name="your.sblock.name" template="Anees_Training::view.phtml"/>
</referenceContainer>
</body>
</page>
Create View File (PHTML): Inside your module's view directory (app/code/Anees/Training/view/frontend/templates
), create your view.phtml
file.
<?php
/** @var \Anees\Training\Block\Index $block */
?>
<?php
echo $block->getCustomMessage();
?>
Access module on frontend domain.com/training/index/index
You can get this module from here.