1.1 Defining your module

Once you've set up Magento and entered it into the root Magento directory, go into app/code/local/. This will be where your module lives on the system. The first folder is your namespace, usually your company name. Then directory underneath this is your module name.

All your module folders will live underneath this module directory. We will create many here, but for starters just create the etc/ and controllers/ directories. We will also create a file called config.xml in this etc/ directory. This is the main, and incredibly important, the configuration file for your module.

Create this folder structure now. Your directory layout should look as follows:

[magentodir]/app/code/local/NAMESPACE/MODULENAME/etc/config.xml

[magentodir]/app/code/local/NAMESPACE/MODULENAME/controllers/

The content of config.xml should be as follows. It adds this module to the rest of the <modules> in the Magento system. And everything is in a module, even the 'core' Magento classes live within modules.

This config.xml specifies the full module name and a version number. The module name, NAMESPACE_MODULE, is derived from the aforementioned folder name. This is a very common pattern in Magento. Take note.

FILE: app/code/local/NAMESPACE/MODULENAME/etc/config.xml

<config>
<modules>
<NAMESPACE_MODULENAME>
<version>0.1.0</version>
</NAMESPACE_MODULENAME>
</modules>
</config>
 
But before the system can actually see your new module you need another config file. This is because the Magento system won't traverse every folder in app/code/local looking for a config.xml file. Instead, it looks in app/etc/modules/ and processes every XML file there. This XML file is similar to the last but without a version number. It specifies that the module is <active>. And that it lives in the local ‘codePool', which means it's in app/code/local. To create this file in app/etc/modules. Its name is the module name as above.

FILE: app/etc/modules/NAMESPACE_MODULENAME.xml

 
<config>
<modules>
<NAMESPACE_MODULENAME>
<active>true</active>
<codePool>local</codePool>
</NAMESPACE_MODULENAME>
</modules>
</config>
 
Now if we flush out the Magento cache in the admin panel (via System->Cache Management->Flush Magento Cache button), and go to System->Configuration->Advanced you should see your module in the list. 
 
 
 
1.2 Declaring your controller 'routes'

But our module doesn't actually do anything currently. We need to add a 'route' going from a URL to a class we define. This will in turn actually do something. Alter your config.xml like so to add that route.

This modification we're adding to the <frontend> of the system (i.e. not the admin panel). And we're adding a router to the other <routers> in the system. We're giving that router a name, useful when dealing with layouts later on.

Then we say it <use>s the ‘standard’ frontend router system. Then it defines the important arguments (<args>): It’ll go to the <module> we’ve just defined. And it's all-important URL, called the <frontName>.

 
FILE: app/code/local/NAMESPACE/MODULENAME/etc/config.xml
 
<config>
<modules>
<NAMESPACE_MODULENAME>
<version>0.1.0</version>
</NAMESPACE_MODULENAME>
</modules>
   <frontend>
<routers>
<ITSROUTERNAME>
<use>standard</use>
<args>
<module>NAMESPACE_MODULENAME</module>
<frontName>URLNAME</frontName>
</args>
</ITSROUTERNAME>
</routers>
</frontend>

</config>
 
 
1.3 Defining the controller class

We’ll now define the PHP class that will be called via that route. Meaning, it will be called when the user goes to <magentourl>/URLNAME.

This will call a class in your controllers/ module directory. The default class that it will call is IndexController.php. And within that class the default method it will call will be indexAction(). All controller class methods called from the outside must have 'Action' suffixed to their name.

If you don’t use these defaults, you have to specify the controller name and action name after URLNAME i.e. [magentourl]/URLNAME/controllername/actionname, as opposed to just [magentourl]/URLNAME. [magentourl]/URLNAME/index/index is the same as just loading [magentourl]/URLNAME

Let’s create that controllers/IndexController.php class now. Its name must be prepended by the Namespace and Module name. All controllers must must have the word Controller at the end. (But you don’t need to use the word 'Controller' should you need to specify the controller in the url.)

The will extend Mage_Core_Controller_Front_Action to allow it to work as a controller and to give it access to various methods and properties of the system.

FILE: app/code/local/NAMESPACE/MODULENAME/controllers/IndexController.php

 
<?php
class NAMESPACE_MODULENAME_IndexController 
               extends Mage_Core_Controller_Front_Action 
   {
public function indexAction() 
       {
echo 'hihi!';
}

   }

?>

So if you go to <magentourl>/URLNAME you should see your new page. It will do nothing except displaying the words 'hihi'. Not impressive--but a start. 

 

You may also want to define a route to the admin panel. But that topic will be dealt with after part I. This is because you will be applying much of what you have learnt dealing with Magento's front end.