Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

In my studio, we have successfully used multi-environment config files to allow several machines on our local network to use local MAMP installs as well as designate dev and production environments at our web host.

Locally we run sites on separate ports, so for instance to look at my site I point my browser to http://10.0.1.2:8081/. To look at what’s going on on each other’s machines in the studio, we just change the ip address.

But now we are looking to run both a stable “trunk” version and a “branch” version for developing new features of our site on each local machine. And I can’t see how to make that work in the general.php and db.php files.

The following is what I’ve had in general.php for running a single instance of the site on my server:

'10.0.1.2' => array(
    'userSessionDuration' => false,

    'environmentVariables' => array(
        'siteUrl'        => 'http://10.0.1.2:8081/',
        'fileSystemPath' => ‘/path/to/trunk/files/‘,
    ),
),

I feel that what I’d like to do is something like the following to differentiate between the two sites running on 10.0.1.2:

'10.0.1.2:8081’ => array(
    'userSessionDuration' => false,

    'environmentVariables' => array(
        'siteUrl'        => 'http://10.0.1.2:8081/',
        'fileSystemPath' => ‘/path/to/trunk/files/‘,
    ),
),

'10.0.1.2:8082’ => array(
    'userSessionDuration' => false,

    'environmentVariables' => array(
        'siteUrl'        => 'http://10.0.1.2:8082/',
        'fileSystemPath' => ‘/path/to/branch/files/‘,
    ),
),

Similarly, in the db.php file, currently I’ve got:

'10.0.1.2' => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => 'trunk.database’
),

Which I thought I’d change to:

'10.0.1.2:8081’ => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => 'trunk.database’
),

'10.0.1.2:8082’ => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => ‘branch.database’
),

But alas these kick off this error:

An exception has been thrown during the rendering of a template ("Craft can’t connect to the database with the credentials in craft/config/db.php.") in "_layout" at line 15.

Am I trying to do something impossible, or can anyone point me in the right direction?

Thank you much, Willhaus

share|improve this question

3 Answers 3

You can define CRAFT_ENVIRONMENT in your index.php like so:

switch ($_SERVER['SERVER_PORT']) {
  case "8081":
    define('CRAFT_ENVIRONMENT', 'TRUNK');
    break;
  case "8082":
    define('CRAFT_ENVIRONMENT', 'BRANCH');
    break;
}

And then in general.php and db.php you can:

return array(
  '*' => array(
    ...
  ),
  'TRUNK' => array(
    ...
  ),
  'BRANCH' => array(
    ...
  )
);
share|improve this answer
    
Great workaround, Marion! Do you also have an idea on how to serve different templates to those environments? Willhaus wants to use "branch" for developing new features, what I think is a great concept for site development. Serving "branch" a different database should work with this config, but I have no clue on how to deal with the templates (see my comments to Brads answer)? –  carlcs Jul 24 at 8:06
    
@marion-newlevant Whoooooaaaaaaaa, thanks! I think that just might do it. It's kind of a drag to edit index.php since it's so nice to keep all your configs together in, duh, craft/config, but I'll take it if it works. I'll report back after I get a chance to test. –  Willhaus Jul 24 at 14:25
    
@christian-seelbach Using Marion's method, I should be able to use fileSystemPath to define my whole separate branch install, allowing for not just separate templates, but plugins, etc also. Thx, and I'll report back if/when I get it working. –  Willhaus Jul 24 at 14:30
    
I think you are wrong, @Willhaus. The custom vars you define in environmentVariables are nowhere as deeply integrated into Craft as you hope. These vars only work in "asset source settings" and "general settings" in the CP. –  carlcs Jul 24 at 15:11
    
You could as well rename fileSystemPath to myWebhostWantsMeToPutMyFilesHerePath, the var name is up to you! –  carlcs Jul 24 at 15:14

When matching the array keys in a multi-environment config, Craft checks the $_SERVER['SERVER_NAME'], which does not include the port, which is in $_SERVER['SERVER_PORT'], so it's currently not going to work the way you expect.

But it will do a partial match (since it just uses strpos) and successfully match the first key if finds. In your case, '10.0.1.2:8081'.

If your MySQL install is running on a non-default (3306) port as well, there is a separate db.php config item for 'port' that you'd need to specify.

share|improve this answer
    
The "fileSystemPath" variable defined in "environmentVariables" is a custom variable. You can probably use it to configure sections' template path in the CP. But how can you have different home pages (or other pages where URI to template path matching is required) bound to the environments, Brad? –  carlcs Jul 24 at 5:51
    
Not sure I'm following the question (it is pretty late here)... currently there is only 3 settings in Craft that support environment variables: buildwithcraft.com/docs/… –  Brad Bell Jul 24 at 6:02
    
As Willhaus wants to serve different templates dependent on the environment, I thought about how to realize that. Using the "fileSystemPath" in the section settings came to mind, but if I interpret you right (it's pretty early here), this is not an option as environment vars won't work there (→ feature request?). –  carlcs Jul 24 at 6:13
    
Maybe a feature request is a good idea. I would suspect there are others who would want the same set up. After all, what I am looking for is already possible easily using "name-based virtual hosting" (is that the right term?). I could use 'trunk.dev’ => array(… and 'branch.dev’ => array(… and it would work perfectly. But that would only work locally – we'd loose the ability to share links through the studio to show our work. I the meantime, I'm going to try Marion's suggestion to edit index.php to capture the ports. –  Willhaus Jul 24 at 14:37
    
Why not add the vhosts to hosts file in /private/etc/ on each machine, @Willhaus? –  carlcs Jul 24 at 15:25
up vote 3 down vote accepted

BOOM! Got it.

The following makes it work. I can leave the ip address as is, and the insert a test for which port I’m on. I never would have figured that without y’all’s help and another pass through the Multi-Environment Configs doc.

I did this for general.php:

'10.0.1.2' => array(
    'userSessionDuration' => false,

    'environmentVariables' => array(
        'siteUrl' => ($_SERVER['SERVER_PORT'] == '8081' ? 'http://10.0.1.2:8081/' : 'http://10.0.1.2:8082/'),
        'fileSystemPath' => ($_SERVER['SERVER_PORT'] == '8081' ? '/path/to/trunk/files/' : '/path/to/branch/files/'),
    ),
),

And this in db.php:

'10.0.1.2' => array(
    'server' => 'localhost',
    'user' => 'root',
    'password' => 'root',
    'database' => ($_SERVER['SERVER_PORT'] == '8081' ? 'trunk.database' : ‘branch.database'),
),

THANK YOU, everyone, for your help!

share|improve this answer
    
Nice catch on the edit ;) –  Willhaus Jul 24 at 18:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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