Simple module creation in Magento

Lemon Kazi
Oceanize Lab Geeks
Published in
2 min readOct 31, 2017

All additions and customizations to Magento are done through modules. So, the first thing you’ll need to do is create a new module. Create an XML file in app/modules named as follows

etc/modules/MyCompanyName_HelloWorld.xml

<?xml version="1.0"?>
<config>
<modules>
<MyCompanyName_HelloWorld>
<active>true</active>
<codePool>local</codePool>
</MyCompanyName_HelloWorld>
</modules>
</config>

MyCompanyName is a unique namespace for your modifications, it doesn’t have to be your company’s name, but that the recommended convention my magento. HelloWorld is the name of your module.

Clear the application cache

Now that the module file is in place, we’ll need to let Magento know about it (and check our work). In the admin application

  1. Go to System->Cache Management
  2. Select Refresh from the All Cache menu
  3. Click Save Cache settings

Now, we make sure that Magento knows about the module

  1. Go to System->Configuration
  2. Click Advanced
  3. In the “Disable modules output” setting box, look for your new module named “MyCompanyName_HelloWorld”

If you can live with the performance slow down, you might want to turn off the application cache while developing/learning. Nothing is more frustrating then forgetting the clear out the cache and wondering why your changes aren’t showing up.

Setup the directory structure

Next, we’ll need to setup a directory structure for the module. You won’t need all these directories, but there’s no harm in setting them all up now.

mkdir -p app/code/local/MyCompanyName/HelloWorld
mkdir -p app/code/local/MyCompanyName/HelloWorld/Block
mkdir -p app/code/local/MyCompanyName/HelloWorld/controllers
mkdir -p app/code/local/MyCompanyName/HelloWorld/Model
mkdir -p app/code/local/MyCompanyName/HelloWorld/Helper
mkdir -p app/code/local/MyCompanyName/HelloWorld/etc
mkdir -p app/code/local/MyCompanyName/HelloWorld/sql

And add a configuration file

app/code/local/MyCompanyName/HelloWorld/etc/config.xml

and inside the configuration file, add the following, which is essentially a “blank” configuration.

<?xml version="1.0"?>
<config>
<modules>
<mycompanyname_helloworld>
<version>
0.1.0
</version>
</mycompanyname_helloworld>
</modules>
</config>

Setting up the router

Next, we need to setup the module’s routers. This will let the system know that we’re handling any URLs in the form of

http://example.com/magento/index.php/helloworld

So, in your configuration file, add the following section.

<config>
<!-- ... -->
<frontend>
<routers>
<!-- the <helloworld> tagname appears to be arbitrary, but by
convention is should match the frontName tag below-->
<helloworld>
<use>standard</use>
<args>
<module>MyCompanyName_HelloWorld</module>
<frontName>helloworld</frontName>
</args>
</helloworld>
</routers>
</frontend>
<!-- ... -->
</config>

What you’re saying here is “any URL with the frontName of helloworld …

So, with the above configuration in place, when you load the helloworld page above, you’ll get a 404 page. That’s because we haven’t created a file for our controller. Let’s do that now.

touch app/code/local/MyCompanyName/HelloWorld/controllers/IndexController.php<?php
class MyCompanyName_HelloWorld_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
echo "We're echoing just to show that this is what's called, normally you'd have some kind of redirect going on here";
}
}
?>

You should now be able to hit the following URLs and see the results of your echo statements

http://example.com/magento/index.php/helloworld/

--

--