First, we will start by creating a simple “Hello world” example module. However, you’ll soon see that simple takes a new meaning with Magento.
If you Creating a bare bone module it requires at least two files in Magento. For your module to work you want /app/etc/modules/YourCompany_MyModulename.xml, app/code/local/YourCompany/MyModule/etc/config.xml. But, barebone module will not give you a “Hello developer”. So, we need to add few more files.
file 1: /app/etc/modules/Techcoder_HelloDeveloper.xml
Each module starts and ends with “config” element. Most simple modules have simply one sub-element under the “config” called “modules”, It holds the definition of individual modules. In our case we have merely one module declared under the “modules” element, called Techcoder_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/local/ folders, /app/code/core/. 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 “Techcoder_HelloDeveloper” element is a mapping to /app/code/local/Techcoder/HelloDeveloper/ location.
file 2: app/code/local/Techcoder/HelloDeveloper/etc/config.xml
Content of file 2 (partial): 0.1.0 With the above two files you should be able to see your module listed under Magento Admin > System Configuration > Advanced > Advanced. Foe now we have our module “working”, but we don’t see no direct results of it is work, no output. So we need to add a bit more to it. First we’ll 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): 0.1.0 standard Techcoder_HelloDeveloper Techcoder-hellodeveloper We needed a way to tell Magento to “use” controllers of a module. Controllers are placed in app/code/local/Techcoder/HelloDeveloper/controllers/ folder. 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/Techcoder-hellodeveloper or http://shop.local/Techcoder-hellodeveloper I would see “Hello developer…”. And if I were to open http://shop.local/index.php/Techcoder-hellodeveloper/index/sayHello I would see ‘Hello one more time…’; file 3: app/code/local/Techcoder/HelloDeveloper/controllers/IndexController.php Content of file 3 (final):
Altough extremely simple, file 3 shows us one important thing: Naming convention. Note the name of the class. Your module 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.