·

photo credit: jurvetson via photopin cc

How to create a Magento module

Since I’ve written various posts now where I assume that the reader already knows how to create a Magento module. But for those who don’t, here’s a little how-to.

The very basic beginning

Actually, to create a Magento module, all you have to do is create one single file. Create a file called Example_Module.xml in app/etc/modules and put the following code in it:

Note the active -node that allows you to enable or disable this module and the codePool -node (with capital P, you silly Magento) which defines in which codepool the module is (originally) located (local, community or core).
That’s it! You now already have created a shiny, new module. Don’t believe me? Just go to the admin, navigate to “System > Configuration > Advanced > Advanced > Disable module output” and behold:

Screenshot 2014-10-29 23.00.36

It’s all about the configuration

Great, but what’s a module without some kind of functionality? Well, I’m glad you asked, because that brings me to the next point: the configuration. Magento is all about configuration, and the main entry point for every module is a file called config.xml located in your module’s etc-folder.
Remember when we called our module file Example_Module.xml ? And you’ve seen the Example_Module -node inside this XML-file? Well, this isn’t without a reason: see the underscore as a separator and then you’ll see that each module can be split up into 2 parts: a package name and a module name. In our example, “Example” is the package name and “Module” is the module name.
So with this information, create the folder app/code/local/Example/Module/etc , and place a file called config.xml  in it. This file is the entry point for our module. Put the following code in it:

This tells Magento that the current version of this module is 1.0.0. This is great if you have install- or update-scripts, but that’s something we’ll cover in another article.

Create a helper

There are a lot of things we can do now, we can define models, blocks, helpers, routers, events, cronjobs, configuration settings, etc. But to keep this article clean and simple, I’ll cover one type of Magento entity here: the helper.
Add the following node to your config.xml -file inside to <config></config> -scope:

These lines tell Magento that when the helper of type “example” is called, the corresponding class is always prefixed with Example_Module_Helper . Models and blocks follow a similar pattern. This means that when I use the following code:

Magento assigns an instance of Example_Module_Helper_Data  to $helper . (A little note here: the /data  part is not required for helpers since the data-helper is the default one. In other words: Mage::helper(‘example’)  would return the same class).
But where must we store this helper? Well that’s simple: in your modules’ folder, create a folder called Helper , and in it put a file called Data.php  with the following code:

That’s it! You now have a module that comes with a handy helper (that does nothing but nobody cares).

In conclusion

Although short, I hope this article showed a little insight on where to start when creating Magento modules. The structure of a module is one, but you can do so many things with modules that if I would wrap it in one article it would probably become one long, dull, boring post. And we don’t want that.
But when it comes to Magento modules, you’re now at least able to:

  • Create a module
  • Create a configuration file for it
  • Create a helper

If there are questions or requests to write another article to highlight another aspect about Magento modules, please say so in the comments.

Visitors give this article an average rating of 4.3 out of 5.

How would you rate this article?

2 thoughts on “How to create a Magento module”

  1. marcobax22 says:

    Great article Giel ! Btw, you can use the shorthand for instantiating the default helper:

    $helper = Mage::helper(‘example’);

    instead of

    $helper = Mage::helper(‘example/data’);

    1. Giel Berkers says:

      I know, I also mentioned that in my article. I thought that for the sake of logic it would be good to mention the complete syntax in the article, so you could understand how the magento factory method is applied.

Leave a Reply