How to Create Controller in Magento 2

23 Nov 2020
As a class located in the module controller folder, the controller is responsible for a specific URL or group of URLs. In Magento 2, the controller is an important part of MVC flow. There are two types of controllers: frontend and admin. They have a similar workflow but the admin controller has additional methods for permission checks.

 

Step 1: Create routes.xml file.

File: app/code/Mageworld/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="Mageworld_HelloWorld"/>

        </route>

    </router>

</config>

 

 

In the previous How to Create Module in Magento 2, we created file routes.xml. If you created it, you can ignore this step.

Step 2: Create controller file

File: app/code/Mageworld/HelloWorld/Controller/Index/Index.php

 

<?php

namespace Mageworld\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);

}

 

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 a response for the request.

Step 3: Create Layout file

File: app/code/Mageworld/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="Magepworld\HelloWorld\Block\Index" name="helloworld_index_index" template="Mageworld_HelloWorld::index.phtml" />

    </referenceContainer>

</page>

Step 4: Create a Block file

File: app/code/Mageworld/HelloWorld/Block/Index.php

<?php

namespace Mageworld\HelloWorld\Block;

class Index extends \Magento\Framework\View\Element\Template

{


}

 

Step 5: Create a template file

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

<h2>My First Extension</h2>

Step 6: Run a test

Let’s open the browser and navigate to

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



Related Posts

How to Set Minimum & Maximum Quantity Allowed in Shopping Cart

How to Set Minimum & Maximum Quantity Allowed in Shopping Cart

13 Nov 2020
Some products are best sold in bulk while others are limited to one per customer. So in order to balance sales and costs, it is important for the website owner to know how to set a limit on the number of orders when selling a product. This article will give you an easy guide to placing minimum / maximum order quantities in Magento 2.
How to Perform Magento 2 Migration: A Step-by-step Guide (2020)

How to Perform Magento 2 Migration: A Step-by-step Guide (2020)

09 Oct 2020
Are you searching for an optimal alternative to your current store because of some technical issues that could make you lose a lot of sales? If yes, you should take Magento 2 into consideration. It’s one of the most powerful eCommerce platforms due to robust built-in features and extreme customizability. Especially, its 2.4 version has just been released with a lot of improvements you cannot ignore. Check out our article here to find out. In this article, I will walk you through an A to Z instruction on performing a successful migration from your current store to Magento 2 with an easy and safe method.
Magento 2.4 Release date: What things you need to notice!

Magento 2.4 Release date: What things you need to notice!

16 Jul 2020
Magento 2.4 is the next major version of the most anticipated e-commerce platform today. Although we have heard many rumors about this version, not to mention the official statement. Therefore, it is expected to include some necessary improvements as well as an entirely new API. So what to expect from the new major update?
How to make Magento 2 performance optimization effectively

How to make Magento 2 performance optimization effectively

20 May 2020
Performance plays a key role in the success of eCommerce stores because it affects many elements: conversion rate, search ranking, customer satisfaction, page views,...