4

To make deploys more efficient our var/ and media/ directories, local.xml and enterprise.xml files are symlinks to actual files outside the webroot. But as far as I know, the files actually have to exist within the Magento tree itself. Rather than using symlinks, is it possible to move where var/ is actually located using some configuration mechanism, such as in local.xml?

7

The directories in Magento are retrieved using this method: Mage::getBaseDir($type='base'). For example you can retrieve the media directory like this: Mage::getBaseDir('media').
This method is a wrapper for Mage::getConfig()->getOptions()->getDir($type);.
Going deeper, I found that these folders are retrieved using the class Mage_Core_Model_Config_Options.
Specific folders like media and var (and others) are initialized in the _construct() method of the class above. You can try to override this method and set the folders as you desire.
With the local.xml and enterprise.xml I think it's a little tricky because they are files and you cannot afford to move the entire folder.
For these files you can try to override this method Mage_Core_Model_Config::loadBase() and make it use your new location for local.xml (and other additional base config files).
I've never tried it but I hope it's a good place to start.

  • Seems like Mage_Core_Model_Config_Options must be instantiated very early – before local.xml is read (by definition), which explains why the locations for these directories can't be configured in local.xml themselves. Thanks! – kojiro Apr 8 '13 at 18:48
3

If you want to force Magento to look directly in another place, you'll have to override the Mage_Core_Model_Config_Options class and modify getVarDir as it does not utilize the settings in app/etc/config.xml to locate the var directory.

However, it is entirely acceptable and not uncommon to use symlinks to locate these directories outside of the web root. Magento does not requires that they be in the root directory and will follow the symlinks. There are a few directories (theme related) where you'd have to enable a follow symlink setting or such for it to follow them, but most of them will work just fine granted the file-permissions are setup correctly for it to work.

1

All the get[type]Dir functions are populated before loading either the XML config files or accessing the Database. Instead of overwriting or extending the existing core classes you can just pass these options in to the Mage::run([$code], [$type], [$options]) call in your index.php (or whatever entry point you have for magento).

You can create an additional php file to make this git friendly (for this example I'm calling it config.php and ignoring by default in .gitignore for local development) and adjust the index.php file as follows.

Index.php

...
$additionalOptions = include_once 'config.php';
if (!is_array($additionalOptions)) {
    $additionalOptions = [];
}

Mage::run($mageRunCode, $mageRunType, $additionalOptions);

config.php

<?php
return [
    'cache_dir' => '/tmp/magento/var/cache',
    'session_dir' => '/tmp/magento/var/session'
];

The options are passed from Mage::run to Mage_Core_Model_App->run($options) and finally to Mage_Core_Model_Config->setOptions($options) where each option overwrites any defaults right before loading the XML config files. All without modifying any Core code.

Though I should mention this works on magento 1.9.2 for sure, I don't have an active instance of 1.7 to test against. @marius may have the best 1.7 compatible answer still.

It should also probably be said that modifying these directories should be done with caution. Once done new developers accustomed to the standard magento structure may be confused initially. One solution to that would be create 2 additional path config files, one part of your repository defining the differences in path structure you've decided to make to the entire project and the second for local environment changes, similar to the why of the config.xml and local.xml magento uses. that way you can point them to a file that tells them all the adjustments you've made.

1

This is overkill, no need to rewrite anything.

see app/etc/local.xml

         <system>
            <filesystem>
                <base>{{root_dir}}</base>
                <app>{{root_dir}}/app</app>
                <code>{{app_dir}}/code</code>
                <design>{{app_dir}}/design</design>
                <locale>{{app_dir}}/locale</locale>
                <etc>{{app_dir}}/etc</etc>
                <media>{{root_dir}}/media</media>
                <upload>{{root_dir}}/media/upload</upload>
                <skin>{{root_dir}}/skin</skin>
                <var>{{var_dir}}</var>
                <cache>{{var_dir}}</cache>
                <session>{{var_dir}}/session</session>
                <tmp>{{var_dir}}/tmp</tmp>
                <pear>{{var_dir}}/pear</pear>
                <export>{{var_dir}}/export</export>
            </filesystem>
        </system>

simply copy the path you want to change to your app/etc/local.xml and change it.

thus to change the location of the var folder you will use

<default>
        <system>
            <filesystem>
                <var>SOME_PATH_OF_YOUR_FILESYSTEM</var>
            </filesystem>
        </system>
</default>
  • This looks exactly what I need, but I'm skeptical. If I change <etc>… how will Magento know where to find the local.xml file? – kojiro Apr 9 '13 at 12:11
  • 1
    Good question, however, I just tried changing location of var and it did not work. I know I have done this before, maybe doing something daft. A bit tired, been a long day. Potentially this does not work?? Anyone used it before to change folder locations maybe shed some light? – ProxiBlue Apr 9 '13 at 12:37

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.