Update: As of Magento 1.5.1.0 there is now an “Allow Symlinks” option that can be found in Advanced -> Developer -> Template Settings that resolves any need for this type of hack.

When I’m developing a module for any given framework, I like to keep my VCS repository clean and only include the files that I would actually package for distribution. The sloppy way to handle this would be to develop my module within the framework, and periodically copy changes to my version controlled files for committing. Instead, I use symbolic links to insert my module files into the framework and only make changes to the original version-controlled files.

Unfortunately Magento doesn’t like this as the template engine explicitly tests file include paths against their real paths and fails if they don’t match. However, we can easily hack the Magento core to bypass this test:

Open up this file:

app/code/core/Mage/Core/Block/Template.php

Navigate to line 213 (as of Magento 1.5.0.1). You should notice the following try/catch block:

try {
    $includeFilePath = realpath($this->_viewDir . DS . $fileName);
    if (strpos($includeFilePath, realpath($this->_viewDir)) === 0) {
        include $includeFilePath;
    } else {
        Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
    }

} catch (Exception $e) {
    ob_get_clean();
    throw $e;
}

To allow symlinks, all we need to do is comment out the if block:

try {
    $includeFilePath = realpath($this->_viewDir . DS . $fileName);
    // if (strpos($includeFilePath, realpath($this->_viewDir)) === 0) {
        include $includeFilePath;
    // } else {
    //     Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
    // }

} catch (Exception $e) {
    ob_get_clean();
    throw $e;
}

Now I can execute my shell script to set up my symlinks. Here’s what my shell script looks like:

export MAGENTO_MODULE_DIR=~/Projects/magento-plugin
export MAGENTO_ROOT_DIR=/www/magento-dev
ln -s -f $MAGENTO_MODULE_DIR/app/code/local/* $MAGENTO_ROOT_DIR/app/code/local/
ln -s -f $MAGENTO_MODULE_DIR/app/design/frontend/base/default/layout/* $MAGENTO_ROOT_DIR/app/design/frontend/base/default/layout/
ln -s -f $MAGENTO_MODULE_DIR/app/design/frontend/base/default/template/* $MAGENTO_ROOT_DIR/app/design/frontend/base/default/template/
ln -s -f $MAGENTO_MODULE_DIR/app/etc/modules/* $MAGENTO_ROOT_DIR/app/etc/modules/
ln -s -f $MAGENTO_MODULE_DIR/app/locale/en_US/* $MAGENTO_ROOT_DIR/app/locale/en_US/
ln -s -f $MAGENTO_MODULE_DIR/skin/frontend/* $MAGENTO_ROOT_DIR/skin/frontend/

Keep in mind it’s not a good idea to do these types of core hacks to any type of production environment.

Share this article:

If this resonates with you I'd love to help!

I help business with problems like these every day.

Click below to schedule a call.

About The Author

Bryce Hamrick

Facebook Twitter

Bryce Hamrick is an entrepreneur, business & marketing strategist, and product consultant with nearly two decades of experience in industry. Bryce has been a software engineer, product manager, and director of product management for startups as well as large enterprises. He has led teams to bring dozens of products to market and has executed numerous six-figure product launches. Today Bryce and his team focus on leveraging his product execution strategy to help businesses with growth and scale.