Tag Archive: module


Modules in Magento allow you to code additional functionality into your magento store. Today I’m making a module for a Pinterest competition!

First thing we do is create the file and folder structure in app/code/local/YOURLIB/YOURMODULE :

app
    /code
        /local
            /Madskull
                /Pinterest
                    /controllers
                    /etc

In the etc folder, create config.xml:




    
        
            0.1.0
        
    


    


        
            
                Madskull_Pinterest_Model
                madskull_pinterest_resource
            
            
                Madskull_Pinterest_Model_Resource
            
        


        
            
                Madskull_Pinterest_Block
            
        


        
            
                Madskull_Pinterest_Helper
            
        


    


    
    


        
            
                standard
                
                    Madskull_Pinterest
                    pinterest
                
            
        



        
            
                
                    madskull/pinterest.xml
                
            
        


    
    

To activate our module, we add a config file app/etc/modules/Madskull_Pinterest.xml :



    
        
            true
            local
        
    

Finally to get up and running add the controller code in controllers/IndexController.php :


    
        
            
                page/1column.phtml
            
        
        
            
        
    

Now reload the page, and you’ll have all the template gubbins rendering too.

Your module is primed and ready for you to start developing!

This was a bit fidgety, but its possible to override core Magento files! Actually there are two ways, but one involves copy pasting the class and tweaking. That will break if the class ever changes on a Magento update, so I won’t even tell you how to do that.

Instead, we’ll create our own module. My module’s called Madskull, and I already have a folder in /app/code/local/madskull

In this example, I overrode Mage_Core_Model_Url. I had to have a custom getUrl() method.

I created (in the aforementioned folder) Url/Url.php, which extends Mage_Core_Model_Url :

<?php

/**
 * Class Madskull_Core_Model_Url
 *
 *  @author delboy1978uk
 */
class Madskull_Url_Url extends Mage_Core_Model_Url
{
    /**
     * Do a quick string replace to ditch apostrophes
     * @return string
     */
    public function getUrl($routePath = null, $params = null)
    {
        return str_replace("'",'',parent::getUrl($routePath,$params));
    }
}

Okay, next in the Url directory, create an etc folder, and create config.xml :

<config>
    <modules>
        <Madskull_Url>
            <version>1.0.1</version>
        </Madskull_Url>
    </modules>
    <global>
        <models>
            <core>
                <rewrite>
                    <url>Madskull_Url_Url</url>
                </rewrite>
            </core>
        </models>
    </global>
</config>

Finally, we activate the module by adding it to a new file /app/etc/modules/Madskull_Url_Url.xml :

<?xml version="1.0"?>
<config>
    <modules>
        <Madskull_Url>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Madskull_Url>
    </modules>
</config>

Job done! Now, without creating second copies of the core classes, you can extend away, and updates will not affect your code!