8

I've just added a secondary site to my Git-bootstrapped EE-site. I've added new upload destinations and I've added them to my file-uploads array in my config.php. My file-uploads array looks like this now (just a stub, obviously there are more like this):

10 => array(
    'name'        => "Blog",                          // Display name in control panel
    'server_path' => $base_path . '/user/blog/', // Server path to upload directory
    'url'         => $base_url . '/user/blog/'      // URL of upload directory
),

With $base_path and $base_url defined as follows:

$base_url                       = $protocol . $_SERVER['HTTP_HOST'];
$base_path                      = $_SERVER['DOCUMENT_ROOT'];

Now this poses a problem: when I administer the secondary site through the admin.php of the primary-site, obviously $base_path and $base_url are going to resolve to that site's domain and document-root. How do I handle this properly? Reason this is causing such a headache is that the secondary site has some issues that cause it's admin.php to not render theme-url properly (forum-thread here: http://expressionengine.com/forums/viewthread/229911/).

Any help would be greatly appreciated!

  • Steven, did any of the answers help you? If yes, please mark the answer correct by clicking on the checkmark to the left of the answer. – Anna_MediaGirl Dec 22 '12 at 6:19
5

So I'm inferring from the forum post you linked that you're bootstrapping your config with something like NSM bootstrap or similar in order to reduce the pain of moving EE between multiple environments (local/staging/production etc.). That's a great idea, and how I roll with EE myself (though take a look at FocusLabs Master Config for another, more in depth approach to this.

However in this particular case I don't think there's a way to avoid hard coding the URL and server paths for each site, given that $_SERVER is necessarily unaware of any sites other than the current execution context, and MSM doesn't even require you to define your domain anywhere, (leaving aside that we need to do all this before EE has loaded anyway).

That said, we can at least make sure you only have to store that information in as few locations as possible, and still make it easy for multiple dev's to have different absolute paths to their local installs etc, by loading per environment config files, based on an environment constant:

define('EE_SERVER_NAME', $_SERVER['SERVER_NAME']);
switch (EE_SERVER_NAME) {
    case 'site1.local';
    case 'site2.local';
      define('EE_ENV', 'local');
      break;
    case 'staging.site1.com';
    case 'staging.site2.com';
      define('EE_ENV', 'staging');
      break;
    case 'site1.com';
    case 'site2.com';
    default;
      define('EE_ENV', 'production');
      break;
  }
}
/* load environment specific config file
 note you could do this inside the switch above, but setting environment
 constants is super useful and gives more flexibility.*/

require_once('./config.'. EE_ENV. '.php');

in config.production.php

define('EE_SITE1_BASE_PATH', '/path/to/site1');
define('EE_SITE1_BASE_URL', 'site1.com');
define('EE_SITE2_BASE_PATH', '/path/to/site2');
define('EE_SITE2_BASE_URL', 'site2.com');

in config.local.php, which is ignored from version control: (include a config.local.example.php to allow new devs to get up and running)

define('EE_SITE1_BASE_PATH', '/different/path/to/site1');
define('EE_SITE1_BASE_URL', 'site1.local');
define('EE_SITE2_BASE_PATH', '/different/path/to/site2');
define('EE_SITE2_BASE_URL', 'site2.local');

upload directories:

//this dir belongs to site 1
9 => array(
    'name'        => "Images",                          // Display name in control panel
    'server_path' => EE_SITE1_BASE_PATH . '/user/images/', // Server path to upload directory
    'url'         => EE_SITE1_BASE_URL . '/user/images/'      // URL of upload directory
),
//this dir belongs to site 2
10 => array(
    'name'        => "Blog",                          // Display name in control panel
    'server_path' => EE_SITE2_BASE_PATH . '/user/blog/', // Server path to upload directory
    'url'         => EE_SITE2_BASE_URL . '/user/blog/'      // URL of upload directory
),

As an aside, you should probably use constants like BASE_PATH instead of $base_url, to prevent them being accidentally reassigned, and to make your code/intent clearer to other developers who might come after you.

I've also assumed that you're not trying to declare two upload destinations (one for each site) as a single upload directory definition in config.php. I'm pretty certain that ain't gonna work...

| improve this answer | |
1

I know this has already been answered by Tom. But if you are using the Focus Lab Master Config, you could modify the /config/config.env.php similar to this:

if ( ! defined('ENV'))
{
    switch (str_replace('www.', '', $_SERVER['HTTP_HOST']))
    {
        case 'mydomain1.local' :
            define('ENV', 'local');
            define('ENV_FULL', 'Local');
            define('ENV_DEBUG', TRUE);
            define('ENV_BASE_PATH', '/Users/yourname/Sites/mydomain1.local');
            define('ENV_BASE_URL', 'mydomain1.local');
        break;

        case 'mydomain1.tld' :
            define('ENV', 'prod');
            define('ENV_FULL', 'Production');
            define('ENV_DEBUG', FALSE);
            define('ENV_BASE_PATH', '/var/www/vhosts/mydomain1.tld');
            define('ENV_BASE_URL', 'mydomain1.tld');
        break;

        case 'staging.mydomain1.tld' :
            define('ENV', 'staging');
            define('ENV_FULL', 'Staging');
            define('ENV_DEBUG', FALSE);
            define('ENV_BASE_PATH', '/var/www/vhosts/staging.mydomain1.tld');
            define('ENV_BASE_URL', 'staging.mydomain1.tld');
        break;

        case 'mydomain2.local' :
            define('ENV', 'local');
            define('ENV_FULL', 'Local');
            define('ENV_DEBUG', TRUE);
            define('ENV_BASE_PATH', '/Users/yourname/Sites/mydomain2.local');
            define('ENV_BASE_URL', 'mydomain2.local');
        break;

        case 'mydomain2.tld' :
            define('ENV', 'prod');
            define('ENV_FULL', 'Production');
            define('ENV_DEBUG', FALSE);
            define('ENV_BASE_PATH', '/var/www/vhosts/mydomain2.tld');
            define('ENV_BASE_URL', 'mydomain2.tld');
        break;

        case 'staging.mydomain2.tld' :
            define('ENV', 'staging');
            define('ENV_FULL', 'Staging');
            define('ENV_DEBUG', FALSE);
            define('ENV_BASE_PATH', '/var/www/vhosts/staging.mydomain2.tld');
            define('ENV_BASE_URL', 'staging.mydomain2.tld');
        break;

        default :
            define('ENV', 'local');
            define('ENV_FULL', 'Local');
            define('ENV_DEBUG', TRUE);
        break;
    }
}

In config.master.php you could change this;

$images_path = $base_path . '/' . $images_folder;

to this;

$images_path = ENV_BASE_PATH . '/' . $images_folder;

I think this would save you from needing to create EE_SITEX_BASE_PATH etc.

| improve this answer | |
  • This is a nice idea - however, you can't really define these vars based on the current HTTP_HOST because they need to work when you switch sites in the EE CP, which doesn't alter the URL. – James Smith Sep 22 '15 at 12:32

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.