Home >How to Create Controller in Magento 2

How to Create Controller in Magento 2

Without a doubt, the controller is one of the integral components in the development process of Magento 2 module. Its main functionalities are to receive, request, process, and render pages. Today, we will guide you on how to create Magento 2 controller in the fastest and safest way.

Create Magento 2 Hello World Module

Step 1: Create a Hello World module directory in the app/code/Bss/HelloWorld

Step 2: Create an etc/module.xml file

In the app/code/Bss/HelloWorld/etc/module.xml by adding the following code:

<?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="Bss_HelloWorld" setup_version="1.0.0">

     </module>

</config>

Step 3: Create an etc/registration.php file

In the app/code/Bss/HelloWorld/registration.php by adding code as below:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(

         \Magento\Framework\Component\ComponentRegistrar::MODULE,

         Bss_HelloWorld',

         __DIR__

);

Step 4: Run command line to enable the module

php bin/magento setup:upgrade

Create Magento 2 Controller

Step 1: Create a routes.xml file

<?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="bss">

            <module name="Bss_HelloWorld"/>

        </route>

    </router>

</config>
 

Please note that:

  • router: frontName – It is the component displayed on URL on accessing the module via browsers.

Here is the example: www…/frontName/actionpath/actionclass/

  • router: id – the one and only name of route id in Magento

router frontName and id are two attributes that might have the same value but are not used for the same purpose.

  • module name: name of the module

Step 2: Create a Magento 2 controller action

Controller/Actionpath/Actionclass.php

  • Actionpath: The directory name inside the Controller directory, the default is index.
  • Actionclass: a class with an execute method to process the request.

In the file app/code/Bss/HelloWorld/Controller/Index/Index.php, please add the code

<?php

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

         }

         public function execute()

         {

                     echo "Hello World";

                     exit;

         }

}

Step 3: Create layout files

In the app/code/Bss/HelloWorld/view/frontend/layout/helloworld_index_index.xml add the code: 

<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> 

         <referenceContainer name="content">

             <block class="Bss\HelloWorld\Block\Index" name="helloworld_index_index"                    template="Bss_HelloWorld::index.phtml" /> 

        </referenceContainer> 

</page>

Step 4: Create a block file

In the  app/code/Bss/HelloWorld/Block/Index.php add the code

<?php

namespace Bss\HelloWorld\Block;

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

{

}

Step 5: Create a template file

In the app/code/Bss/HelloWorld/view/frontend/templates/index.phtml add the code

<h2>Hello World!!!!!</h2>

Step 6: Run Command

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento cache:flush

Above, we discuss how to create a controller in Magento 2. If you have any question, feel free to leave it below.

P/S: Stay tuned with our Magento knowledge base series to get more and more useful information.

< Previous Post
Next Post >
+ posts