alias.io

I'm Elbert, a Dutch expatriate programmer in Australia. I use and create open-source software.

Currently employed as Technical Director at Tundra Interactive, a digital agency in Melbourne.

Contact

honeypot@alias.io PGP Key

Creating a Magento module, part I

Published on November 8, 2012

This article describes how to create a bare bones Magento module from scratch.

Getting started

Magento's code is organised in modules, most of which can be extended or overridden. This allows for customisations without modifying the core code.

1. Create a module namespace

Create a directory app/code/local/[Namespace], where "Namespace" is typically the name of the company you work for. Note that the local folder may not yet exist.

The namespace neatly groups your modules together and separates them from others.

2. Create a module directory

Create a directory app/code/local/[Namespace]/[ModuleName].

3. Create a configuration file

Create a subdirectory and file at app/code/local/[Namespace]/[ModuleName]/etc/config.xml.

<?xml version="1.0"?>
<config>
  <modules>
    <[Namespace]_[ModuleName]>
      <version>1.0.0</version>
    </[Namespace]_[ModuleName]>
  </modules>
</config>

Substitute the values between square brackets with your own. The XML tag must match the path to your module, replacing slashes with underscores.

4. Create a module file

Create a file at app/etc/modules/[Namespace]_[ModuleName].xml. This is to make Magento aware of the module and its whereabouts.

The filename is largely irrelevant in this case. For instance you could name it [Namespace]_All and define multiple modules.

<?xml version="1.0"?>
<config>
  <modules>
    <[Namespace]_[ModuleName]>
      <active>true</active>
      <codePool>local</codePool>
    </[Namespace]_[ModuleName]>
  </modules>
</config>

To completely disable the module at any time change the value of <active> to false in this file.

5. Clear caches

The easiest way to clear caches in a development environment is by manually deleting the contents of var/cache/. Doing so will cause Magento to reload the configuration files and the module becomes active.

If everything went well the module shows up in System > Configuration > Advanced > Advanced.

Troubleshooting

More often than I'm willing to admit modules don't work the first time. Magento chooses to silently ignore errors in XML files which makes debugging unnecessarily difficult.

  • Check for typos. Case matters (even more so on UNIX-like systems) and XML tags should be properly closed.
  • Double check the directory structure and make sure all files exist.
  • Make sure the caches are disabled or cleared. Clear them again.
  • Sometimes permissions can be an issue. Directories need to be executable and files readable by the web server.

Continued at Creating a Magento module, part II (Model).