Magento needs no explaning, but it does need a shitload of proper developers education. So much that our team spent a good amount of time trying to learn how to develop on the damn thing. So heres a synthesized description of our efforts. We’re breaking up this HOWTO into a few segments into increasing orders of complexity and integration.

Download the source for this entire series here!

Let us begin …

Setup Module Environment and Helloworld

We’re trying to achieve a proof of concept here to describe how the file structure, namespaces and config files interplay to achieve a Hello world message

  1. Create your directory structure! Our NEW company name is Savant Degrees! and our module shall be called….. Twits (for a lack of an imagination). The whole module (well, almost) should rest in the /app/code/local> folder. So lets first create the following folder structure. For a hello world module, you only need a config file, a controller and a block. Ergo…
    app/
      etc/
        modules/
          - SavantDegrees_All.xml (Or what ever your company name might be)
      code/
        local/
          SavantDegrees/ (Or what ever your company name might be)
            Twits/ (Or whatever your module name might be)
              Block/
                - HelloWorld.php
              controllers/
                - IndexController.php
              etc/
                - config.xml
  2. Create your config.xml file. Thats the crux of the whole module.
    <?xml version="1.0"?>
    <config>
        <modules>
            <SavantDegrees_Twits>
                <version>0.1.0</version>
            </SavantDegrees_Twits>
        </modules>
        <global>
            <blocks>
                <twits>
                    <class>SavantDegrees_Twits_Block</class>
                </twits>
            </blocks>
        </global>
        <frontend>
            <routers>
                <SavantDegrees_Twits>
                    <use>standard</use>
                    <args>
                        <module>SavantDegrees_Twits</module>
                        <frontName>twits</frontName>
                    </args>
                </SavantDegrees_Twits>
            </routers>
        </frontend>
    </config>

    Let me explain this step a little…

    The modules > [module] > version number allows Magento to track when your module is upgraded. We’ll be seeing a version of that shortly.
    The global > blocks > [module] define the Block resources that are available under your package. We’ll be using this to generate a Hello world shortly
    The frontend > routers fragment define how the module can be access. The current code allows the block to be access via https://127.0.0.1/magento/index.php/twits

  3. Create your IndexController.php file. Its as simple as:
    class SavantDegrees_Twits_IndexController extends Mage_Core_Controller_Front_Action
    {
     
        public function indexAction()
        {
    	$this->loadLayout();
    	$this->getLayout()
    		->getBlock('content')->append(
    			$this->getLayout()->createBlock('twits/helloWorld')
        	);
            $this->renderLayout();
        }
    }

    More Explanation: The twits/index refers to the index block inside the Twits module, which is defined in the config file above. We’re trying to put its contents inside the layout.

  4. Lastly, we’ll create the Index.php Block:
    class SavantDegrees_Twits_Block_HelloWorld extends Mage_Core_Block_Template
    {
    	protected function _toHtml()
    	{
    		return 'Hello world';
    	}
    }

    You DO understand SOME PHP don’t you?

  5. Finally, we activate the module by creating the file SavantDegrees_All.xml file
    <?xml version="1.0"?>
    <config>
        <modules>
            <SavantDegrees_Twits>
                <active>true</active>
                <codePool>local</codePool>
            </SavantDegrees_Twits>
        </modules>
    </config>
  6. Wake up the Magento installer by logging into the admin backend, System > Configuration > Advanced > Advanced. Click on Save Config. Clear your cache at /var/cache too just to be sure.

Tada! Navigate to https://127.0.0.1/magento/index.php/twits to see your well deserved Hello World!

» Next … Part 2 – Admin Controller


9 Responses to “Howto: Repackageable custom extension development in Magento”

  1. Mark Says:

    As Jim said before, you have some case-sensitivity issues in your code above. Make sure IndexControler.php and HelloWorld.php start with upper-case letters (they still have lower-cased first characters in the directory tree).

    This is pretty important for a system that heavily recommends installation on *NIX systems instead of Windows.

  2. Serge Says:

    Thanks a lot.
    It’s the simplest way to start programming magento among those I’ve found in www

  3. Eyad Says:

    go ahead man

    thanks
    i get the prob the problem was with th digram

  4. Eyad Says:

    hello

    thanks for your helpful . but i did what you explain >> i don’t know why i get ERROR 404
    i tried to do it again >>> but the same prob i met

    could you get me the reason if u can

  5. Matt Says:

    This is a very helpful guide. Thank you!
    In my case I was getting a Magento error ‘Controller file was loaded but class does not exist’ until I noticed there was no opening ‘<?php’ in the IndexController.php file. I added that, voila! Progress.

  6. gaweee Says:

    Thanks! I’ve updated the code to reflect those changes!

  7. Jim Says:

    o0o I worked it out … in your directory structure diagram you have indexController.php however later on in your code you have IndexController.php
    notice the capital I… the I needs to be capital.. mine was lower case thanks again

    err ps.. I posted this follow up on part 4 two by mistakes :$

  8. Jim Says:

    Hi there,

    Thanks for a great tutorial, I’am having some troubles though…

    I followed the tutorial on this page step by step for hello world… however changed the company and module name.. the new module shows up in config>advanced and is enabled, I then clear cache and tried to access it from demoshop/mymodname and I get the magento 404 message..

    So I start over, not changing any names or anything.. keep the code exactly as you have it above… and it again appears in config as enabled, so I reset cache and try to access demoshop/twit and agian get the magento 404…

    any assistance on this would be much thought of :)

    many thanks,
    Jim

  9. Claudia Says:

    Thank you – you tutorial is the first one I found where everything just works and has a good explanation as well. Thanks again.

    Claudia

Leave a Reply