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!

Advertisements