Magento 1 – base directories
With base directories, Magento offers a very simple solution to get Magento directories. If you are writing a module that should work on other Magento installations, you need to depend all files on this base directory system. You may put media files to media directory, JavaScript files to js folder or temporary files to var.
Magento 1 – base directories
Magento base directories are very useful if you want to work with Magento file structure (read or write files). You should not use absolute or relative hard coded paths, because it is dangerous. You don’t know if your hoster changes something or if you want to move your store to another server. It is recommended to always use base directories, especially if you write modules. You can use this paths everywhere:
- in your controllers
- models
- blocks
- .phtml files
- own files ans scripts that use Magento core
Here is a list of all possible base directory that you may use. Be aware, that all paths are without a trailing slash at the end!
Complete list of Mage::getBaseDir paths
These are all directories of Magento default installation you can get from getBaseDir function. getBaseDir hat two params. The first one is the directory name, the second is the module name. If you only use the first param, you will get base installation directories:
- base directory
/path/to/magento1$path = Mage::getBaseDir('base'); - app directory
/path/to/magento/app1$path = Mage::getBaseDir('app'); - code directory
/path/to/magento/app/code1$path = Mage::getBaseDir('code'); - design directory
/path/to/magento/app/design1$path = Mage::getBaseDir('design'); - locale directory
/path/to/magento/app/locale1$path = Mage::getBaseDir('locale'); - lib directory
/path/to/magento/lib1$path = Mage::getBaseDir('lib'); - media directory
/path/to/magento/media1$path = Mage::getBaseDir('media'); - upload directory
/path/to/magento/media/upload1$path = Mage::getBaseDir('upload'); - skin directory
/path/to/magento/skin1$path = Mage::getBaseDir('skin'); - var directory
/path/to/magento/var1$path = Mage::getBaseDir('var'); - tmp directory
/path/to/magento/var/tmp1$path = Mage::getBaseDir('tmp'); - cache directory
/path/to/magento/var/cache1$path = Mage::getBaseDir('cache'); - log directory
/path/to/magento/var/log1$path = Mage::getBaseDir('log'); - session directory
/path/to/magento/var/session1$path = Mage::getBaseDir('session'); - export directory
/path/to/magento/var/export1$path = Mage::getBaseDir('export');
You can also get some Magento module directories. For this you need to set the module name for the second param:
- module directory
/path/to/magento/app/code/core/Mage/Core1$path = Mage::getBaseDir('', 'Mage_Core'); - module config directory
/path/to/magento/app/code/core/Mage/Core/etc1$path = Mage::getBaseDir('etc', 'Mage_Core'); - module controller directory
/path/to/magento/app/code/core/Mage/Core/controllers1$path = Mage::getBaseDir('controllers', 'Mage_Core'); - module locale directory
/path/to/magento/app/code/core/Mage/Core/locale1$path = Mage::getBaseDir('locale', 'Mage_Core'); - sql directory
/path/to/magento/app/code/core/Mage/Core/sql1$path = Mage::getBaseDir('sql', 'Mage_Core');
All examples are for Mage_Core, but you can also use third party modules here!
Conclusion
Magento offers you a simple solution to get base directories. Use it!