Mage Check Facebook icon Mage Check Linkedin icon

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:
http://example.com/route_name/controller/action

  • 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: Install Module\Extension

Install the module from initial page

Step 2: Declare Controller

Magento 1 (2007 - 2014)

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


        
    

Magento 2 Open Source (2014 -)

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


        
    

Step 3: Create Function

Magento 1 (2007 - 2014)

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


        
    

Magento 2 Open Source (2014 -)

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


        
    

Step 4: Put Changes Into Effect

Magento 1 (2007 - 2014)

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

Magento 2 Open Source (2014 -)

Now we need to deploy!

bin/magento setup:upgrade
bin/magento setup:di:compile

Step 5: Git Sources

Magento 1 (2007 - 2014)

URL: https://bitbucket.org/magecheck/magento1945/src/create-frontend-controller/

git clone -b create-frontend-controller git@bitbucket.org:magecheck/magento1945.git

Magento 2 Open Source (2014 -)

URL:

Will be added further

Step 6: Test

Notice
Open your favourite browser and give your site this route:
https://example.com/tutorial/index/index
And now, this path will call your controller function and display the result.