Mage Check Facebook iconFacebook MAGE CHECK Mage Check Linkedin iconLinkedin MAGE CHECK Mage Check Skype iconSkype MAGE CHECK

Magento How to create a Frontend Controller

$ In Magento Controller has one or more files in Controller folder of module, it includes actions of class which contain execute() method.

$ How Controller works?
It receives a request from end-user (browser or comamnd line), for example:

  • route_name is a unique name which is set in routes.xml
  • controller is the folder inside Controller folder
  • action is a class with execute method to process request

Step 1: Declare Controller

Magento 1

<?xml version='1.0'?> <config> <modules> <MageCheck_Tutorial> <active>true</active> <codePool>local</codePool> </MageCheck_Tutorial> </modules> </config>

File path: app/etc/modules/MageCheck_Tutorial.xml

Magento 2

<?xml version='1.0'?> <config xmlns:xsi='' xsi:noNamespaceSchemaLocation='../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd'> <router id='standard'> <route id='tutorial' frontName='tutorial'> <module name='MageCheck_Tutorial' /> </route> </router> </config>

File path: app/code/MageCheck/Tutorial/etc/frontend/routes.xml

Step 2: Configure Module

Magento 1

<?xml version='1.0'?> <config> <modules> <MageCheck_Tutorial> <version>0.1.0</version> </MageCheck_Tutorial> </modules> <frontend> <routers> <tutorial> <use>standard</use> <args> <module>MageCheck_Tutorial</module> <frontName>tutorial</frontName> </args> </tutorial> </routers> </frontend> </config>

File path: app/code/local/MageCheck/Tutorial/etc/config.xml

Magento 2

<?xml version='1.0'?> <config xmlns:xsi='' xsi:noNamespaceSchemaLocation='urn:magento:framework:Module/etc/module.xsd'> <module name='MageCheck_Tutorial' setup_version='1.0.2'/> </config>

File path: app/code/MageCheck/Tutorial/etc/frontend/module.xml

Step 3: Create Function

Magento 1

<?php class MageCheck_Tutorial_IndexController extends Mage_Core_Controller_Front_Action { public function indexAction() { echo 'Welcome to MageCheck Tutorials - Magento 1' . '<br><br>'; $this -> createControllerMethod(); } public function createControllerMethod() { echo 'Your Frontend Controller is Working!'; } }

File path: app/code/local/MageCheck/Tutorial/controllers/IndexController.php

Magento 2

<?php namespace MageCheck\Tutorial\Controller\Index; use \Magento\Framework\App\Action\Context; class Index extends \Magento\Framework\App\Action\Action { /** * @param Context $context */ public function __construct( Context $context ){ parent::__construct($context); } public function execute() { // here are the functions you want to be displayed in controller; } public function function_name() { // here you put the code; } }

File path: app/code/MageCheck/Tutorial/Controller/Index/Index.php

Step 4: Register and Test Controller

Magento 1

Log in to Magento Admin panel, go to System -> Cache Management, Flush Magento Cache and that's it!

This function's instructions will be executed and magento will return the result to the user browser.

Magento 2

<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'MageCheck_Tutorial', __DIR__ );

The last thing you'll have to do is to Flush your cache.

File path: app/code/MageCheck/Tutorial/registration.php

Open your favourite browser and give your site this route:
And now, this path will call your controller function and display the result.

This is the sample way to create a Frontend Controller in Magento!

Contact us

Have you decided on an online Business Plan website?