How to Create Controller in Magento 2?
We have previously discussed in detail on how to create a new module in Magento 2. In this blog, we will discuss how to create a controller and use it in frontend.
What is a controller Magento 2?
Basically, Controller is a class which is created in Controller folder, and it includes files which have execute() method.
To create a controller in Magento 2, follow the below steps:
Step 1 – Create routes.xml file
First, let’s create a routes.xml file in the app/code/Mageants/Blog/etc/frontend and add the following code.
<?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=”Mageants_Blog”/> </route> </router> </config> |
For use Controller in Magento backend then you can create routes.xml file in app/code/Mageants/Blog/etc/adminhtml and add the following code:
<?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=”admin”> <route frontName=”helloworld” id=”helloworld”> <module name=”Mageants_Blog”/> </route> </router> </config> |
Step 2 – Create a Controller file
After creating routes.xml file, we will create a controller file “Index.php” in the app/code/Mageants/Blog/Controller/Index and add the following code.
<?php namespace Mageants\Blog\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(); } } |
All Controllers must be extended \Magento\Framework\App\Action\Action class. In execute() method, we will write our code.
Step 3 – Create a Layout file
Now, we will create a layout file “helloworld_index_index.xml” in the app/code/Mageants/Blog/view/frontend/layout and add the following code.
<?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=”Mageants\Blog\Block\Index” name=”helloworld_index_index” template=”Mageants_Blog::index.phtml” /> </referenceContainer> </page> |
Step 4 – Create a Block file
Now, we need to create a Block file “Index.php” in the app/code/Mageants/Blog/Block and add the following code.
<?php namespace Mageants\Blog\Block; class Index extends \Magento\Framework\View\Element\Template { } |
Step 5 – Create a Template file
Finally, we need to create a template file “index.phtml” in the app/code/Mageants/Blog/view/frontend/templates and add the following code.
<?php echo “Welcome to Mageants”; ?> |
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:///helloworld/index/index
Recommended Read: Create Custom API in Magento 2 Step By Step
Conclusion:
Using above blog, you can easily understand How to Create Controller in Magento 2. If you have any query regarding above code implementation, contact us.