Magento2 controller dispatch

Magento2 Controller

In MVC type frameworks controllers are very important to handle the incoming request and sending response. In this article we are going to see the magento2 controller dispatch in detail. As we all know normally in magento 1.x to execute any controller in modules we need the following things

  • module router
  • Controller file
  • layout file
  • template file
  • Block file to use any business logic in template file

In magento2 also we are going to follow the same procedural steps but the rules for defining the syntax are vary.


The routes are necessary to define the frontname of the module. In magento 1.x the frontname routes are defined in config.xml. But in magento the routes have to be delcared in separate file routes.xml in the frontend directory of etc folder.

  <!-- File: app/code/Explorer/Test/etc/frontend/routes.xml -->
   <config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="explorertest" frontName="explorertest">
            <module name="Explorer_Test" />

All the frontend router comes with ‘standard’ id. When declaring the route frontname use the same as ‘id’ which will be used for full throughout of module.


Next step is to create the controller file. The majore difference we are going to see only in the controller file. In magento 1.x normally the controller can be extended by Mage_controller_Front_Action then we need the action method to create a page in controller. To extend the dyamic designs with logic in page we need the handler for action. But in Magento2 controllers there is no action method. Every controller should have execute funtion to execute it. To extend the layout in controller we need to go with pagefactory class, which is similar to loadlayout() and renderlayout(); functions in magento 1.x. The sample test controller code is

#File: app/code/Explorer/Test/Controller/Sample/Test.php
namespace Explorer\Test\Controller\Sample;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class Test extends \Magento\Framework\App\Action\Action
    protected $pageFactory;
    public function __construct(Context $context, PageFactory $pageFactory)
        $this->pageFactory = $pageFactory;
        return parent::__construct($context);
    public function execute()
        $page_object = $this->pageFactory->create();
        return $page_object;



To use the controller handler we need the layout file for it. In magento 1.x we have this layout file normally in app/design/frontend/base/default folder but in magento2 these files are come under the module directory itself. A new directory called ‘view’ in the module was introduced in magento2, which will have all the frontend layout and template files of the corresponding module.

   <!-- File: app/code/Explorer/Test/view/frontend/layout/explorertest_sample_test.xml -->
	<?xml version="1.0"?>
<page xmlns:xsi="" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">        
    <referenceBlock name="content">
        <block template="content.phtml" class="Explorer\Test\Block\Main" name="explorertest_test" />

Then the important thing in layout is declaring the block class, in magento 1.x we used block class with the parameter ‘type’ but here that should be declared as the ‘class’ itself.


As explained above, the template files also come under the view directory of module.

#File: app/code/Explorer/Test/view/frontend/templates/content.phtml
	<h1>Test controller</h1>


To use any business logic in template files we need to call the block function there. In magento 1.x normally the block class will extend magento core template that differed as framework view element template. The sample code for block class

#File: app/code/Explorer/Test/Block/Main.php
namespace Explorer\Test\Block;
use Magento\Framework\View\Element\Template;
class Main extends Template
    protected function _prepareLayout()