Previously I already wrote how to build custom module (extension) in Magento. Here is a new, refreshed article on that subject.
We will start by creating a simple “Hello world” module. However, you will soon see that simple takes a new meaning with Magento. Creating a bare bone module requires at least two files in Magento. For your module to work you need /app/etc/modules/MyCompany_MyModule.xml, app/code/local/MyCompany/MyModule/etc/config.xml. But, barebone module will not give you a “Hello developer” 🙂 -So, we need to add few more files to the game.
file 1:
/app/etc/modules/Inchoo_HelloDeveloper.xml
/app/etc/modules/Inchoo_HelloDeveloper.xml
content of file 1:
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_HelloDeveloper>
<active>true</active>
<codePool>local</codePool>
</Inchoo_HelloDeveloper>
</modules>
</config>
Each module starts and ends with “config” element. Most simple modules have merely one sub-element under the “config” called “modules”, It holds the definition of individual modules. In our case we have only one module declared under the “modules” element, called Inchoo_HelloDeveloper. Not the “two part” name the module has. It is a practise (and necessity) to call your modules by MyCompanyName plus TheModuleItSelf. There are three places in the system where modules can reside: /app/code/community/, /app/code/core/, /app/code/local/ folders. Take a look at file 1 content for a moment. Notice the “codePool” element? Its value is currently set to “local”, meaning our module config instructed the module to look for module files in /app/code/local/ folder. Whereas “Inchoo_HelloDeveloper” element is a mapping to /app/code/local/Inchoo/HelloDeveloper/ location.
file 2: app/code/local/Inchoo/HelloDeveloper/etc/config.xml
content of file 2 (partial):
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_HelloDeveloper>
<version>0.1.0</version>
</Inchoo_HelloDeveloper>
</modules>
</config>
Content of file 2 is the base module config file. With the above two files you should be able to see your module listed under Magento Admin > System Configuration > Advanced > Advanced. For now we have our module “working”, but we do not see no direct results of its work, no output. So we need to add a bit more to it. First we will expand config.xml file giving it some extra parametars to “turn on” controllers so that I can call them from frontend.
content of file 2 (final):
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_HelloDeveloper>
<version>0.1.0</version>
</Inchoo_HelloDeveloper>
</modules>
<frontend>
<routers>
<Inchoo_HelloDeveloper_SomeFreeRouterName1>
<use>standard</use>
<args>
<module>Inchoo_HelloDeveloper</module>
<frontName>inchoo-hellodeveloper</frontName>
</args>
</Inchoo_HelloDeveloper_SomeFreeRouterName1>
</routers>
</frontend>
</config>
Notice the additions we added to final file!? We needed a way to tell Magento to “use” controllers of a module. Controllers are placed in app/code/local/Inchoo/HelloDeveloper/controllers/ folder.
And to wrap this one up, we will add one more file, the cntroller we would like to call from the frontend. As you might notice there is an element called “frontend” within wich we define routers. Routers are defined in sub-element “routers” with a few additional parametars. Notice the element “Inchoo_HelloDeveloper_SomeFreeRouterName1”? This is merely a router name. It can be freely assigned. Parametar “use” can have value “admin” or “standard”, while element “args” mainly holds two additional parametars, “module” and “frontName”. Element (param) “frontName” is most interestig since its value defines the url on which the controller will be accessible. In our example, if I were to open url http://shop.local/index.php/inchoo-hellodeveloper or http://shop.local/inchoo-hellodeveloper I would see “Hello developer…”. And if I were to open http://shop.local/index.php/inchoo-hellodeveloper/index/sayHello I would see ‘Hello one more time…’;
file 3: app/code/local/Inchoo/HelloDeveloper/controllers/IndexController.php
content of file 3 (final):
<?php
class Inchoo_HelloDeveloper_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
echo 'Hello developer...';
}
public function sayHelloAction()
{
echo 'Hello one more time...';
}
}
?>
Altough extremely simple, file 3 shows us one important thing: Naming connvention. Note the name of the class. Your moudle classes should keep names in form of MyCompany_MyModule_FileName, or in case of block and modules: MyCompany_MyModule_Block_FileName or MyCompany_MyModule_Module_FileName.
Thats it for “simple hello world” module.
Very nice blog. Thanks for posting amazing blog. Looking for Magento Services and Software for your business and industries, you can visit https://apaengineering.com/technology/pim-softwares/pmt/
ReplyDelete