🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Create Route and Controller in Magento 2

By Jignesh ParmarUpdated on Apr 23, 2025 3 min read

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.

The action is an actual controller class in which there will be an execute() method.

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.

<?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

<?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.

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.

How to Create Route and Controller in Magento 2

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.

Read more:

Jignesh Parmar Full Image
Article byJignesh 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.