Compatibility: Magento 1

In this tutorial series we will create Magento module.

This tutorial is good for beginners who want to learn how to write Magento modules. This tutorial assumes that you have already set up your local environment (Apache and MySQL) and that you have Magento Community Edition (or Enterprise) up and running on your machine. In this tutorial we will assume that the URL of you Magento homepage is www.magento.local We are going to create a module which will write "Hello World" in the URL www.magento.local/helloworld

This is quite easy. All you need to do is to perform the 4 following steps:

  • create the folders of the path: app/code/local/Mypackage/Helloworld/ This is where your module files will be located.
  • create the file: app/etc/modules/Mypackage_Helloworld.xml in the file contents, put the following:
    
    <?xml version="1.0"?>
    <config>
    <modules>
        <Mypackage_Helloworld>
            <active>true</active>
                <codePool>local</codePool>
            </Mypackage_Helloworld>
        </modules>
    </config>
    This file tells Magento about the existence of your module. It tells Magento that the module is active (you can deactivate your module by simply changing the "true" to "false" in the "active" tag), and that the module resides in the "local" directory (i.e app/code/local).
  • create the file: app/code/local/Mypackage/Helloworld/etc/config.xml in the file contents, put the following:
    <?xml version="1.0"?>
    <config>
        <modules>
            <Mypackage_Helloworld>
                <version>1.0.0</version>
            </Mypackage_Helloworld>
        </modules>
       <frontend>
            <routers>
                <mypackage_helloworld>
                    <use>standard</use>
                    <args>
                        <module>Mypackage_Helloworld</module>
                        <frontName>helloworld</frontName>
                    </args>
                </mypackage_helloworld>
            </routers>
       </frontend>
    </config>
    This is the configuration file of your module. For now, it is very simple, but it will get more complicated as your module will grow. There are currently two parts in the config file - "version" and "routers". The "version" is the version of your module, and you can put any number in x.x.x format there. The "routers" is needed for Magento to know that the URL www.magento.local/helloworld will use your module code.
  • create the file: app/code/local/Mypackage/Helloworld/controllers/IndexController.php in the file contents, put the following:
    <?php
    class Mypackage_Helloworld_IndexController extends Mage_Core_Controller_Front_Action{
        public function indexAction()
        {
            echo "Hello World!!!!";
        }
    } this is the code that Magento will run, when the user will go to www.magento.local/helloworld for Magento, the URL www.magento.local/helloworld is the same like www.magento.local/helloworld/index/index Which is in turn searches for the controller named "IndexController.php" inside the "Helloworld" module, and to run the "indexAction" method.

Now, lets refresh the cache by going to the admin panel (usually located at www.magento.local/admin), "System->Cache Management" and click "Flush Magento Cache" Now go to www.magento.local/helloworld and you should see: Hello World!!!!

Great job!

Now Magento knows that it should run your code for www.magento.local/helloworld, and you can start work on the business logic.

0

Comments

Leave A Reply

Your email address will not be published.