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/ActiveCodeline_HelloDeveloper.xml
content of file 1:
<?xml version="1.0"?> <config> <modules> <ActiveCodeline_HelloDeveloper> <active>true</active> <codePool>local</codePool> </ActiveCodeline_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 ActiveCodeline_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 “ActiveCodeline_HelloDeveloper” element is a mapping to /app/code/local/ActiveCodeline/HelloDeveloper/ location.
file 2: app/code/local/ActiveCodeline/HelloDeveloper/etc/config.xml
content of file 2 (partial):
<?xml version="1.0"?> <config> <modules> <ActiveCodeline_HelloDeveloper> <version>0.1.0</version> </ActiveCodeline_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> <ActiveCodeline_HelloDeveloper> <version>0.1.0</version> </ActiveCodeline_HelloDeveloper> </modules> <frontend> <routers> <ActiveCodeline_HelloDeveloper_SomeFreeRouterName1> <use>standard</use> <args> <module>ActiveCodeline_HelloDeveloper</module> <frontName>activecodeline-hellodeveloper</frontName> </args> </ActiveCodeline_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/ActiveCodeline/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 “ActiveCodeline_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/activecodeline-hellodeveloper or http://shop.local/activecodeline-hellodeveloper I would see “Hello developer…”. And if I were to open http://shop.local/index.php/activecodeline-hellodeveloper/index/sayHello I would see ‘Hello one more time…’;
file 3: app/code/local/ActiveCodeline/HelloDeveloper/controllers/IndexController.php
content of file 3 (final):
<?php class ActiveCodeline_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.
Thanks for your blog Branko! It is much easier to get started in Magento development with your blog than using the official sites documentation.
You are doing a really great job, thanks for sharing so much information.
Can you please help me that how I can customize the related products section in admin?
Like suppose I want to add one more field, how I can do that.
Thanks in advance
Nice refresh on your site. I like the hello world through the controllers. It just shows there’s a million way to make Magento say hello world.
@all Thnx for all the feedback.
Hi,
I created one module as per above coding.But I couldn’t see my “Hello developer… “.please your valuable reply
@Rojan Not sure how helpful can I be on such a “no detailed info” feedback. Things to check… .htaccess, url rewrite, sMaLlLettERs in configs, bunch of other stuff can go wrong. All I can say is that module is working. Try going trough it again. Cheers.
have to enable controller in admin side ? like module enabling process.
@RojanJohn I suggest first you read all the available materials on official Magento site related to modules, controllers, models, overrides, and so on. Also, try setting up a basic Zend application will help in understanding of general concepts. Cheers.
This is really great. I did my first module in magento. Its really so much useful and descriptive so I created my own controllers and added different actions.
Great.
PK
I'm probably doing something daft here, but I can't get the actions to work for me. I get “Hello developer…” but the sayHello action is not working. I'm adding to this so that can add customer, order etc entites, load them and view them with a core dump, but all my actions “not found”. I've seen on other wiki's and posts a section to add actions in the config.xml, is that what i'm missing?
Other than that, Thanks as always Branko, been reading your site for 3 weeks and just about everything I know comes from you, thanks again.
Great post, really got me started on the right track. Thank you.
I am new for Magento. I don't know how to run new module in Magento. after add a new module, where we can find the module in magento. Please give the explanation.
Keep up the good work, nice tutorial
Good post nice and easy to follow
Thanks Branko for the detailled Instruction.
Unfortunatly it doesn't work for me. I get a 404, whem I try to open the Site. And I also can't see the Extension in the Backend under Admin > System Configuration > Advanced > Advanced.
It's a totally fresh Magento-Instllation.
Any idea, what i'm doing wrong?
Cheers,
vada
Thanks Branko for the detailled Instruction.
Unfortunatly it doesn't work for me. I get a 404, whem I try to open the Site. And I also can't see the Extension in the Backend under Admin > System Configuration > Advanced > Advanced.
It's a totally fresh Magento-Instllation.
Any idea, what i'm doing wrong?
Cheers,
vada
This is exactly what I have been looking for, thanks. I will try it out for sure, keep it up!
@Vada :
Hi vada, try to empty the cache of Magento through the backend area. it should be okay after that.
Hi..i also applied the same steps u said branko,but not able to see the desired output that u said…please give ur precious time,look into this,iam not able see the output in the home page of my local host,actually tell where to see the output and how(i refreshed the home page but did not saw the “Hello Devloper” output)
thanks….
Thank you. Very much appreciated, clear, clean ans concise.
Did you refresh the cache in the admin panel? Any time you edit / create xml files in magento you HAVE to refresh the cache, even if it's disabled. Go to “System -> Cache Management”.
Hope this helps
Hi,
Thanks for the excellent tutorial. I'm having trouble getting it to work, even though I copied and pasted the files. I've double checked the filenames.
My issues is that I don't even see the module in Config/System/Advanced/Advanced. I'm using Magento 1.4.0.1.
Is there a log file or someplace I can look to see what modules are loaded? Do I need to rebuild some index or somehow notify Magento of the new module? Restart apache?
Any assistance would be greatly appreciated.
Thanks!
Whoops! Andrew's comment about refreshing the cache solved my problem.
Great! thanks!
what the hell its not working for me…
I could not get it working in Magento 1.4.1.1
Is it working in 1.4 at all?
Thank you/