Hikmadh Commerce| Ecommerce Development Consulting | Magento Development and Consulting Service

How to create a controller and its actions in Magento2

Controller specially is one of the important thing in Module development series, and PHP MVC Framework in general. It functionarity 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. They 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?

It receive an request from end-user (browser or comamnd line), for example:

<http://example.com/route_name/controller/action"?>


  1. route_name is a unique name which is set in routes.xml.
  2. controller is the folder inside Controller folder.
  3. action is a class with execute method to process request.


One of the important in Magento system is frontController (Magento\Framework\App\FrontController), it alway receives request then route controller, action by route_name Let’s take an example of routing an request:


<foreach ($this->_routerList as $router) {
   try {
      $actionInstance = $router->match($request);
   …
}>
																


If there is an action of controller class found, execute() method will be run.

How to create a controller in Magento 2?


To Create Controller in Magento 2:

  • Step 1: Create routes.xml file
  • Step 2: Create controller file
  • Step 3: Create controller Layout file
  • Step 4: Create controller Block file
  • Step 5: Create controller template file
  • Step 6: Flush Magento cache
  • Step 7: Run a test new controller


To create a controller, we need to create a folder inside Controller folder of module and declare an action class insideit. For example, we create a index controller and a index action for module Hikmadh_HelloWorld:


Step 1: Create routes.xml file.


File: app/code/Hikmadh/HelloWorld/etc/frontend/routes.xml


                                        

<?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 frontName="helloworld" id="helloworld">
            <module name="Hikmadh_HelloWorld"/>
        </route>
    </router>>
</config>


Step 2: Create controller file


File: app/code/Hikmadh/HelloWorld/Controller/Index/Index.php
<?php 

namespace Hikmadh\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
	protected $_pageFactory;

	public function __construct(
		\Magento\Framework\App\Action\Context $context,
		\Magento\Framework\View\Result\PageFactory $pageFactory)
	{
		$this->_pageFactory = $pageFactory;
		return parent::__construct($context);
	}

	public function execute()
	{
		return $this->_pageFactory->create();
	}
}

														
				

As you see, all controllers must be extended\Magento\Framework\App\Action\Action class which has dispatch method which will call execute() method in action class. In this execute() method, we will write all of our controller logic and will return response for the request.

 

Step 3: Create Layout file

File: app/code/Hikamdh/HelloWorld/view/frontend/layout/helloworld_index_index.xml


<?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">
    <referenceContainer name="content">
        <block class="Hikmadh\HelloWorld\Block\Index" name="helloworld_index_index" template="Hikmadh_HelloWorld::index.phtml" />
    </referenceContainer>
</page>


Step 4: Create Block file


File:app/code/Hikmadh/HelloWorld/Block/Index.php
<?php
namespace Hikmadh\HelloWorld\Block;
class Index extends \Magento\Framework\View\Element\Template
{
}

Step 5: Create template file


File: app/code/Hikmadh/HelloWorld/view/frontend/templates/index.phtml


<h2>Welcome to Magento Controller< /h2 >


Step 6: Flush Magento cache


Step 7:Run a Test


http://<yourhost.com>/helloworld/index/index


OR

http://<yourhost.com> /helloworld