Guide to programming with MagentoMagento code is awesome. So awesome that 20 wiki entries and a 100 files open later i’m was still lost. So much that i ordered the PHP|architects Magento book! At first i was disappointed in its coverage but it did help me out along the way. 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.

Note: Apparently i’ve been giving our dear readers the misconception that the book is what taught me all these techniques. WRONG. The book’s lack of coverage is what prompted me to write this. I’m not sure i was any better off reading the book! :(

If you have a really good idea of what you’re doing/not doing, you could cut right through the chase and download the entire tutorial’s code here: Magento Tutorial

Otherwise, let us begin …

Do us all a favour and get your cache management right (disabled) from the beginning
Go to your system backend: http://127.0.0.1/magento/index.php/admin
Login and click ok System > Cache Management
Then on the All Cache dropdown menu, select Disable
Save cache settings

Create barebones code, file structure and config
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….. Twit (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)
            Twit/ (Or whatever your module name might be)
              Block/
                - Index.php
              controllers/
                - IndexController.php
              etc/
                - config.xml
  2. Create your config.xml file. Thats the crux of the whole module.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    <?xml version="1.0"?>
    <config>
        <modules>
            <SavantDegrees_Twit>
                <version>0.1.0</version>
            </SavantDegrees_Twit>
        </modules>
        <global>
            <blocks>
                <twit>
                    <class>SavantDegrees_Twit_Block</class>
                </twit>
            </blocks>
        </global>
        <frontend>
            <routers>
                <SavantDegrees_Twit>
                    <use>standard</use>
                    <args>
                        <module>SavantDegrees_Twit</module>
                        <frontName>twit</frontName>
                    </args>
                </SavantDegrees_Twit>
            </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/twit

  3. Create your IndexController.php file. Its as simple as:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    
    <?php
    class SavantDegrees_Twit_IndexController extends Mage_Core_Controller_Front_Action
    {
     
        public function indexAction()
        {
    	$this->loadLayout();
    	$this->getLayout()
    		->getBlock('content')->append(
    			$this->getLayout()->createBlock('twit/index')
        	);
            $this->renderLayout();
        }
    }

    More Explanation: The twit/index refers to the index block inside the Twit 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:
    1
    2
    3
    4
    5
    6
    7
    8
    
    <?php
    class SavantDegrees_Twit_Block_Index 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
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    <?xml version="1.0"?>
    <config>
        <modules>
            <SavantDegrees_Twit>
                <active>true</active>
                <codePool>local</codePool>
            </SavantDegrees_Twit>
        </modules>
    </config>
  6. Wake up the Magento installer by logging into the admin backend, System > Configuration > 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/twit to see your well deserved Hello World!


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

  1. Serge Says:

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

  2. Eyad Says:

    go ahead man

    thanks
    i get the prob the problem was with th digram

  3. 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

  4. 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.

  5. gaweee Says:

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

  6. 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 :$

  7. 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

  8. 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