Mohamed Abbas | Architect Magento | Tech Blogger | Magento Trainer

Mohamed Abbas
Mohamed Abbas
Architect Magento | Tech Blogger | Magento Trainer

How Magento 2 Controllers Work

Creating a controller in Magento 2 involves setting up various files and configurations to handle incoming requests, process them, and render responses. Magento 2 controllers come in two types: frontend (for customer-facing operations) and backend (for admin operations). While they share a similar setup, backend controllers often include permission checks like form keys.

How Magento 2 Controllers Work
Controllers receive and process requests from users (e.g., web browsers). The FrontController (Magento\Framework\App\FrontController) is the entry point for requests, determining the appropriate route, controller, and action method.
Example URL:

				
					http://example.com/route_name/controller/action

				
			
  • route_name: Defined in routes.xml

  • controller: Subfolder inside the Controller directory

  • action: Class with an execute() method that processes the request
  • Steps to Create a Controller in Magento 2

    Step 1: Create the routes.xml File

    This file defines the route name and links it to the module.

    File: app/code/Abbas/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="Abbas_HelloWorld"/>
        </route>
    </router>
</config>

				
			
  • Step 2: Create the Controller File

    In this example, we’ll create a controller Index with an execute() method to define the main functionality.

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

				
					<?php 

namespace Abbas\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;
        parent::__construct($context);
    }

    public function execute()
    {
        return $this->_pageFactory->create();
    }
}

				
			

This controller extends Magento\Framework\App\Action\Action and defines the execute() method, where you can add logic to process the request.

  • Step 3: Create the Layout XML File

    Define the page layout and template block for the controller.

    File: app/code/Abbas/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="Abbas\HelloWorld\Block\Index" name="helloworld_index_index" template="Abbas_HelloWorld::index.phtml" />
    </referenceContainer>
</page>

				
			
  • Step 4: Create the Block Class

    This block class will handle data and business logic for your template.

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

				
					<?php

namespace Abbas\HelloWorld\Block;

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

				
			
  • Step 5: Create the Template File

    This .phtml template file defines what is displayed on the page.

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

				
					<h2>Welcome to Magento Controller</h2>

				
			
  • Step 6: Flush Magento Cache

    Clear the cache to make sure the new controller and other configurations are loaded.

				
					bin/magento cache:flush
				
			
  • Step 7: Test the Controller

    Visit the URL:

				
					http://example.com/helloworld/index/index

				
			

Or use the shorter format:

 
				
					http://example.com/helloworld
				
			

Thanks for reading

Picture of Mohamed Abbas

Mohamed Abbas

Technical Lead | Magento Architect