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.
How controller work? www.example.com/route_name/controller/action
route_name is a unique name which is set in routes.xml
.
controller is the folder inside Controller folder.
action is a class with execute method to process request.
Step 1 : Create routes.xml
file.
File Path : app/code/V4U/Helloworld/etc/frontend/routes.xml
1 2 3 4 5 6 7 8 |
<?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"> <route id="helloworld" frontName="helloworld"> <module name="V4U_Helloworld"/> </route> </router> </config> |
Step 2: Create controller file
File Path : app/code/V4U/Helloworld/Controller/Index/Index.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 |
<?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 Index 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 Index Page. * * @return \Magento\Framework\View\Result\Page */ public function execute() { return $this->_resultsPageFactory->create(); } } |
Step 3: Create Layout file
File Path : app/code/V4U/Helloworld/view/frontend/layout/helloworld_index_index.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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> V4U-How to Create Controller in Magento 2? </title> </head> <body> <referenceContainer name="content"> <block class="V4U\Helloworld\Block\Helloworld" name="helloworld" template="V4U_Helloworld::helloworld.phtml" /> </referenceContainer> </body> </page> |
Step 4: Create Block file
File Path : app/code/V4U/Helloworld/Block/Helloworld.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 Helloworld 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 5: Create template file
File Path : app/code/V4U/Helloworld/view/frontend/templates/helloworld.phtml
1 2 3 |
<h2> <?php echo $block->getHelloWorld(); ?> </h2> |
Step 6: 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/index/
or www.example.com/helloworld/
Happy Coding. Keep Liking & Sharing