Controllers in Magento 2, like on other PHP MVC frameworks, are important part of MVC flow. In Magento 2 controllers have only one method (execute) that will be called by front controller. It functionality is that received request, process and render page. In Magento 2 Controller has one or more files in Controller folder of module, it includes actions of class which contain execute() method.
There are 2 different controllers, they are frontend controller and backend controller.
Difference between admin and front controller : Main difference between these two controllers is in additional check and additional methods in admin controller. Both controllers eventually extend \Magento\Framework\App\Action\Action
class, but admin controller extend \Magento\Backend\App\Action
class, which extends \Magento\Framework\App\Action\Action
. Both are generally similar of workflow, but admin controller is a little different. There is a checking permission method in admin controller, it calls form key.
You can call template file from a controller. Follow the below steps.
Step 1 : Create controller file
File Path : app/code/V4U/Helloworld/Controller/Index/View.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php namespace V4U\Helloworld\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\RequestInterface; use Magento\Framework\View\Result\PageFactory; class View extends Action { /** * The PageFactory to render with. * * @var PageFactory */ protected $_resultsPageFactory; /** * Set the Context and Result Page Factory from DI. * @param Context $context * @param PageFactory $resultPageFactory */ public function __construct( Context $context, PageFactory $resultPageFactory ) { $this->_resultsPageFactory = $resultPageFactory; parent::__construct($context); } /** * Show the Hello World View Page. * * @return \Magento\Framework\View\Result\Page */ public function execute() { $resultPage = $this->_resultsPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__(' heading ')); $block = $resultPage->getLayout() ->createBlock('V4U\Helloworld\Block\View') ->setTemplate('V4U_Helloworld::view.phtml') ->toHtml(); $this->getResponse()->setBody($block); } } |
Step 2: Create Block file
File Path : app/code/V4U/Helloworld/Block/View.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace V4U\Helloworld\Block; class View extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Backend\Block\Template\Context $context, array $data = [] ) { parent::__construct($context, $data); } public function getHelloWorld() { return 'Hello World'; } } |
Step 3: Create template file
File Path : app/code/V4U/Helloworld/view/frontend/templates/view.phtml
1 2 3 |
<h2> <?php echo $block->getHelloWorld(); ?> </h2> |
Step 4: Run the following Magento commands :
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
Step 7: Launch the url : www.example.com/route_name/controller/action
Ex : www.example.com/helloworld/index/view/
For More Info download this module.
Happy Coding. Keep Liking & Sharing