Create Controller In Magento2

Create a Controller in Magento 2

Magento Follow the MVC architecture to Manage modules in the system. So in today’s Blog, I will show you how we can create a Controller in Magento 2.

Unlike the Magento 1 controller, Magento 2 Controller is different in Manners of the Action. In Magento 1 you can define multiple actions in the single-action file but in Magento 2 you have to create different Action files for different actions of the Controller. I will show you how we can do that.

There are 2 different types of controllers in Magento.

  1. frontend controller
  2. backend controller

In this blog, I will show you how we can Create a frontend controller and in the future blog will show you how we can create a backend controller.

How to create a controller in Magento 2?

To create a controller in Magento 2 We have to follow below steps.

  1. Step 1: Create a new module.
  2. Step 2: Add a routes.xml file.
  3. Step 3: Add a controller (action) file.

1. Create a new module.

Check how to Create a Module in Magento 2 to create a new module.

2. Add a routes.xml file.

Url strucutre of the controller divided in 3 parts.

    1. frontname
    2. controller
    3. action
  • fronname

frontname is define in routes.xml which is uniqe identifier for the controller.

  • controller

controller is the folder inside Controller folder of module. you may have many controller in single fronname or you can say single module.

  • action

action is a class with execute method to process request.

Create routes.xml file in etc/frontend/  folder and paste below code.

Path: app/code/Rk/SimpleModule/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="simplemodule" id="simplemodule">
            <module name="Rk_SimpleModule"/>
        </route>
    </router>
</config>

3. Add a Controller (action) file.

Add controller(action) file under Controller folder of your module and paste below content.

Path: app/code/Rk/SimpleModule/Controller/Index/Index.php

Note: Index is a default Controller and Action name. If you  in url like http://www.example.com/simplemodule it will call Index/Index.php file by default

<?php


namespace Rk\SimpleModule\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    public function execute()
    {
        die("Message from the Simple Controller");
    }
}

after Creating all the files run below commands.

if you have already enabled the extension then ignore setup:upgrade command. You can check all Magento 2 Useful CLI Command List 

php bin/magento setup:upgrade

and then clean the cache.

php bin/magento cache:clean

Now, Our controller is created and when you hit in url http://www.yourhost.com/simplemodule you will see out put like “Message from the Simple Controller”.

Hope you find this Tutorial is helpful. Do comment if you face any issue or Contact me If you want any help or customization in your existing Project or Extension.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top