5

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

Willhaus
  • 201
  • 1
  • 9

3 Answers3

4

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(
    ...
  )
);
Marion Newlevant
  • 12,047
  • 22
  • 55
  • 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 '14 at 08: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 '14 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 '14 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 '14 at 15:11
  • You could as well rename fileSystemPath to myWebhostWantsMeToPutMyFilesHerePath, the var name is up to you! – carlcs Jul 24 '14 at 15:14
  • Have you ever used a multi-environment config? What I'm describing is exactly why that feature exists. The only difference is that I want 2 of my environments to be on the same machine running on different ports. – Willhaus Jul 24 '14 at 15:52
  • Alas, @marion-newlevant, I can't get that to work. I get the same 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. error as before. You were referring to the index.php in the rot of the content folder, right? – Willhaus Jul 24 '14 at 16:20
  • Oooof, sorry if that soured a little harsh, @christian-seelbach. What I meant to impart was that if config/general.php would pay attention to the port, 'fileSystemPath' => ‘/path/to/trunk/files/‘ and 'fileSystemPath' => ‘/path/to/branch/files/‘ do a fine job of pointing to different sets of craft install files. – Willhaus Jul 24 '14 at 16:56
  • It wasn't clear to me that you sync those versions and end up having two totally separate Craft installs for "trunk" and "branch" on your machine, @Willhaus! The way I use a "multi-environment" config for local development is to have ONE INSTALL and configure the files to allow access from different clients (craft.dev same machine, 10.0.1.2 network, craft.dyndns.org external). – carlcs Jul 24 '14 at 19:04
  • Additionally I can eg. enable/disable robots or Craft caching dependent on the environment and I thought that maybe there is a way to serve different templates from that one install to my environments as well? Then all one has to do is to add another environment (eg. branch.willhaus.dev) and test new layout/features with these files. – carlcs Jul 24 '14 at 19:04
3

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.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
  • 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 '14 at 05: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: http://buildwithcraft.com/docs/multi-environment-configs#setting-support – Brad Bell Jul 24 '14 at 06: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 '14 at 06: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 '14 at 14:37
  • Why not add the vhosts to hosts file in /private/etc/ on each machine, @Willhaus? – carlcs Jul 24 '14 at 15:25
  • Oh, man, do mean map each ip + port to each vhost on each machine? I guess that would work if I could figure it out, but it sure does seem more cumbersome than editing 2 config files synced via version control. – Willhaus Jul 24 '14 at 16:11
  • @Willhaus, I didn't mean this to be a replacement for your environmental configs in Craft. In my opinion the convenience is definitely worth it! You'd have branch.willhaus.dev instead of 10.0.1.2:8082 (and before you ask, yes I've ever used bookmarks!). – carlcs Jul 24 '14 at 19:05
3

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!

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Willhaus
  • 201
  • 1
  • 9