How to Create Route and Controller in Magento 2
In Magento 2, when you execute code to see the output on the frontend, the action method is used.
To show any message on the frontend while developing Magento 2 extension, the concepts of controller, route and actions are the fundamental things you need to understand.
In order to display a message on the frontend, routes.xml and Index.php are important files that every Magento extension developer has to create.
For instance,
http://example.com/route_name/controller/action
Let us understand the path.
A route_name is a unique name that you have to set in routes.xml file for executing the action.
The controller is a class that is located in the controller folder.
Therefore, to execute and run your first Magento 2 extension, let us create routes.xml file.
Method to Create Route and Controller in Magento 2:
Create routes.xml in Magento 2:
app/code/Meetanshi/Extension/etc/frontend/routes.xml
Create routes.xml on this path.
1 2 3 4 5 6 7 8 9 |
<?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="extension" id="extension"> <module name="Meetanshi_Extension"/> </route> </router> </config> |
Now, we are going forward for creating a controller file that is Index.php.
Create Index.php Controller in Magento 2:
app/code/Meetanshi/Extension/Controller/Index/Index.php
Utilising the path, use the code given below for creating 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 |
<?php namespace Meetanshi\Extension\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Magento\Framework\App\Action\Action; class Index extends Action { protected $resultPageFactory; public function __construct(Context $context, PageFactory $resultPageFactory) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; } public function execute() { echo("Meetanshi Extension"); } } |
Here, the execute() method is called when the router matches with the controller action class.
After creating this file, clear the cache memory. You can also clear cache from the below command.
1 |
php bin/magento cache:clean |
Now, type the URL based on your domain.
For example: http://example.com/extension/index/index
Based on the path, either you execute on the localhost or live site, the output will be displayed.
By executing the module, Meetanshi Extension will be printed on the frontend.
If you get the output without any error, that means you have developed controller and route.xml file properly.
Congratulation! You have got your first Magento 2 extension running successfully.
Do consider sharing this blogpost with beginners who are passionate about learning Magento extension development.
Thank you.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Create Module in Magento 2
How to Fix Unique Constraint Violation Found in Magento 2
Next