How to Create Controller in Magento 2

  • March 5, 2023
No Comments

To create Controller in Magento 2 we need to create basic module with router file routes.xml in etc/frontend/routes.xml

Create basic module with your Vendorname and Modulename , for example i am using my company name and Learning as module name

1.) Create folder Magentoservices and then create another folder Learning inside Magentoservices folder.
2.) Create registration.php module registration file inside Learning folder with below contents. your can replace the modulename and vendorname with yours.


<?php
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Magentoservices_Learning',
    __DIR__
);

?>

3.)Create module.xml file inside Magentoservices/Learning/etc/module.xml with below contents.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magentoservices_Learning">
    </module>
</config>

4.) Create routes.xml file inside Magentoservices/Learning/etc/frontend/routes.xml with below contents. frontname you can use what you want on frontend url

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="learning" frontName="m2revision">
            <module name="Magentoservices_Learning" /> 
        </route>
    </router>
</config>

5.) Create a Controller file .

app/code/Magentoservices/Learning/Controller/Index/Index.php

Create Controller and Index folder inside Magentoservices/Learning/Controller/Index/ and add file Index.php with below contents.


<?php
namespace Magentoservices\Learning\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);
	}

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

6.) Now our module controller is ready so now we have to create the xml layout file and template file for module.

Create Layout file

app/code/Magentoservices/Learning/view/frontend/layout/learning_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="Magentoservices\Learning\Block\Index" name="learning_index_index" template="Magentoservices_Learning::index.phtml" />
    </referenceContainer>
</page>

7.) Create Block file for module with below contents

app/code/Magentoservices/Learning/Block/Index.php

<?php
namespace Magentoservices\Learning\Block;
class Index extends \Magento\Framework\View\Element\Template
{

}

8.)Now create template file with the name you definded inside layout xml.

app/code/Magentoservices/Learning/view/frontend/templates/index.phtml with below contents.

<h2>Welcome to magento-services.co.uk</h2>
<p>We will continue learning all magento codes with magento standards which will help you to become expert magento 2 developer.</p>

9.) Now Navigate to root of your magento 2 installation and run below commands

After following above steps, run below commands

  • php bin/magento setup:upgrade
  • php bin/magento setup:di:compile
  • php bin/magento setup:static-content:deploy -f
  • php bin/magento cache:flush

After run above commands, please check output in your browser with help of below URL:

http:///Yourdomain.com/m2revision

http:///Yourdomain.com/frontname

we are expert in creating controller in Magento 2 so feel free to contact us 

About us and this blog

We are a digital magento 2  development agency with a focus on helping our customers achieve great results across several key areas.

More From Our Blogs

See all posts

Leave a Comment