In this tutorial we going to see How to Create Controller in Magento 2, we will go through each and every point step by step.
Magento 2 Controller contain one or many more files in controller folder module, it also includes actions of class which contain execute method.It contain 2 different controllers, which is called frontend controller and backend controller. Which is generally similar of workflow, but admin controller is little different. It also a checking permission method in admin controller, it calls form key.
How controller work?
It receive an request from end-user.
For example: http://example.com/route_name/controller/action
- route_name: It is a unique name that is set in routes.xml.
- controller: It is the folder inside Controller folder.
- action: It is a class with an execute method to process requests.
In the Magento system is frontController, always receives request then route controller and action by route_name Below example of routing an request:
foreach ($this->_routerList as $router) {
try {
$actionInstance = $router->match($request);
}
There is also an action of controller class found, in which execute() method will be run.
To understand more about regarding how to Create Controller in Magento 2 we will do a practice.
Step 1: Create a new module called Techone_ControllerTutorial
- Create the namespace Techone in the path app\code. - Create the module name ControllerTutorial in the path app\code\Techone. - Create the file named registration.php in the path app\code\Techone\ControllerTutorial
<?php
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Techone_ControllerTutorial', __DIR__);
- Create the file name module.xml in the path app\code\Techone\ControllerTutorial\etc
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Techone_ControllerTutorial" >
</module>
</config>
End of step #1, I have been completed the step to create new module called Techone_ControllerTutorial
Step 2: Declare routes.xml file
- To create a controller, we need to create a folder inside Controller folder of module and declare an action class inside it. For example, we create a index controller and a index action for module Techone_ControllerTutorial. - Create the file name routes.xml in the path app\code\Techone\ControllerTutorial\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="controllertutorial" id="controllertutorial">
<module name="Techone_ControllerTutorial"/>
</route>
</router>
</config>
Step 3: Declare controller file
- Create new file named Index.php in the path app\code\Techone\ControllerTutorial\Controller\Index\Index.php
<?php
namespace Techone\ControllerTutorial\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 4 : Declare Layout file
- Create new file named controllertutorial_index_index.xml in the path app\code\Techone\ControllerTutorial\view\frontend\layout\controllertutorial_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="Techone\ControllerTutorial\Block\Index" name="controllertutorial_index_index" template="Techone_ControllerTutorial::index.phtml" />
</referenceContainer>
</page>
Step 5: Declare Block file
- Create new file named Index.php in the path app\code\Techone\ControllerTutorial\Block\Index.php
<?php
namespace Techone\ControllerTutorial\Block;
class Index extends \Magento\Framework\View\Element\Template
{
}
Step 6: Declare template file
- Create new file named index.phtml in the path app\code\Techone\ControllerTutorial\view\frontend\templates\index.phtml
<h2>Controller created successfully</h2>
Step 7: Test and see the results
Run the command lines following: php bin/magento setup:upgrade php bin/magento setup:static-content:deploy -f php bin/magento cache:flush
Let’s open browser and navigate to
http://<domain.com>/controllertutorial/index/index
or
http://<domain.com>/controllertutorial/
Check below practical session for controller in magento 2