1

Mage::getModuleDir seems to be a widely-used way to build requires (for example, for overriding a controller). But Magento itself seems to use relative paths, relying on the opening lines of Mage.php:

/**
 * Set include path
 */
$paths = array();
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
$paths[] = BP . DS . 'lib';

$appPath = implode(PS, $paths);
set_include_path($appPath . PS . Mage::registry('original_include_path'));

As long as the code I'm overriding is in 'local', 'community', 'core' or 'lib', what need is there to use getModuleDir? For example, I might write:

require_once(Mage::getModuleDir('controllers', 'Mage_Customer') . DS . 'AccountController.php');
class My_Customer_AccountController extends Mage_Customer_AccountController
{}

Or I could just write

require_once('Mage/Customer/controllers/AccountController.php');
class My_Customer_AccountController extends Mage_Customer_AccountController
{}

What is better about one or the other?

2

First a bit of background.
The standard router method Mage_Core_Controller_Varien_Router_Standard::getControllerFileName() also uses Mage::getModuleDir('controllers', $realModule) to build the full path to the router in question. It does not use the include path.
That also is the reason why the include path hack (that is, copying a class into a higher priority code pool) doesn't work with controller classes.

To answer your question, following the core router design is the first reason not to use a relative path.

A second reason is that the assumption about the include path may be wrong. For example, its different if the Magento compiler is enabled.

Also, some third party modules (like the experimental branch of the magento-composer-installer) enable module installation outside of the Magento Base Dir, in which case it would be important to use the Mage::getModuleDir() method of retrieving a modules directory.

To summarize, I think relying on the include path for anything but classes loaded by the Magento Autoloader is a bad idea.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.