Jump to content

Support for config-localhost.php file (or similar)


Lance O.
 Share

Recommended Posts

I typically work with three hosting environments: local, stage (on external server), and live.

PW currently supports config-dev.php and config.php. I'd love to see support for an additional config file such as:

config-stage.php
config-test.php
config-review.php

config-local.php
config-localhost.php

or something similar. It would make it much easier to manage three hosting environments, each with their own database.

  • Like 1
Link to comment
Share on other sites

  • 4 months later...
On 11/17/2022 at 12:08 AM, Lance O. said:

I typically work with three hosting environments: local, stage (on external server), and live.

PW currently supports config-dev.php and config.php. I'd love to see support for an additional config file such as:

config-stage.php
config-test.php
config-review.php

config-local.php
config-localhost.php

or something similar. It would make it much easier to manage three hosting environments, each with their own database.

Im currently using this setup to manage this exact case you mention, my folder structure is looking like this for all my environments:
 

site
├── config.php
└── config-dev.php

My config.php has the usual stuff and a constant definition to detect my current environment like this:

<?php
# All the usual processwire configurations

# Have my database setup with env vars:
$config->dbHost = $_ENV['dbHost'];
$config->dbName = $_ENV['dbName'];
$config->dbUser = $_ENV['dbUser'];
$config->dbPass = $_ENV['dbPass'];
if ($_ENV['dbHostReader']) {
   $config->dbReader = [
      'host' => $_ENV['dbHostReader']
   ];
}
$config->dbPort = '3306';

/*------------------------------------*\
 Detect environment
\*------------------------------------*/
switch ($_SERVER['HTTP_HOST']) {

   case 'www.site.com':
      define('ENV', 'PRODUCTION');
      break;
    
   case 'staging.site.com':
      define('ENV', 'STAGING');
      break;

   default:
      define('ENV', 'LOCAL');
      break;
}

Since config-dev.php is present on all my environments, it will prevent config.php from running, so I manually call it and then override the settings I need like this:

# I call the normal config.php so that the usual stuff is loaded as always
require_once 'config.php';

# In case env vars failed
if (!$config->dbHost) die('Missing env vars');


if ('LOCAL' === ENV) {
  $config->moduleInstall('download', true);

  // tracy config for dev development
  $config->tracy = [
    'outputMode' => 'development',
    'guestForceDevelopmentLocal' => true,
    'forceIsLocal' => true, // use this only on local dev!!!!
  ];

  /**
   * Disable all HTTPS requirements?
  */
  $config->noHTTPS = true;

}

if ('STAGING' === ENV) {
	# Staging settings...
}

This way I can use the exact same codebase on all my environments, hope this helps

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

@ArklogicFor actual PW versions, just put your live config settings into config.php and your local development settings into config-dev.php and don‘t upload the dev config to your live site. This works quite well for small sites/teams. In config-dev.php you could use plain PHP to read from .env or add switches to reflect different stages based on a variable, flag etc.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...