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 work?

It receive an 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

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

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

Magento 2

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

<?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>

Step 2: Configure Module

Magento 1

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

<?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>

Magento 2

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

<?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>

Step 3: Create Function

Magento 1

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

<?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!'; } }

Magento 2

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

<?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; } }

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

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

<?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.

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!