Magento How to create a extension module


To show you how to create a Magento extension, we are going to build a Tutorial module, which is going to have only the sample structure. Let's follow the step below!


Step 1: Declare Extension


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/registration.php

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

Step 2: Create Configuration Files


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/module.xml

<?xml version='1.0'?> <config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='../../../../../lib/internal/ Magento/Framework/Module/etc/module.xsd'> <module name='MageCheck_Tutorial' setup_version='0.0.0'> </module> </config>

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

<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:noNamespaceSchemaLocation='../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd'> <router id='admin'> <route id='magecheck_tutorial' frontName='magecheck_tutorial'> <module name='MageCheck_Tutorial' /> </route> </router> </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>'; // call yourFunction(); } public function yourFunction() { // ... } }

Magento 2


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

<?php namespace MageCheck\Tutorial\Controller\Index; class Test extends \Magento\Framework\App\Action\Action { public function __construct( \Magento\Framework\App\Action\Context $context ){ return parent::__construct($context); } public function execute() { echo 'MageCheck Tutorial!'; } }

Do not forget to clean Magento Cache.


This is the sample way to create an Extension in Magento!