Jump to content

Using PHP include in config.php


ethanbeyer
 Share

Recommended Posts

To me, saving database credentials to a Git repository, even if that repo is private, is a big no-no. So I've been tinkering with different ways to deploy sites to production environments without saving the database credentials to the repo. I know that there is a the config-dev.php option. But that hasn't worked for me, as I will explain later.

I added this to my config.php:

 

$config->env = "dev";

if($config->env == "dev") {
	$config->dbHost = 'localhost';
	$config->dbName = 'processwire';
	$config->dbUser = 'root';
	$config->dbPass = 'root';
	$config->dbPort = '3306';
} else {
	require("./{$config->env}.config.php");
}

So if $config->env = "production", production.config.php should be loaded:

$config->dbHost = 'localhost';
$config->dbName = 'processwire-production';
$config->dbUser = 'username';
$config->dbPass = 'productionPassWord';
$config->dbPort = '3306';

I can't see anything wrong with the syntax of this at all - but for some reason, if that file is added with require() or require_once(), ProcessWire never loads. If it's loaded with include(), I get an error about the page not being found, and there being no install.php present.

So it seems like for some reason, there is no way to include a file in config.php, which confuses me immensely.

 

How my brain works

I'll try to explain my thought process behind this a little more.
I think it's a good idea for dev environments (especially those that come with a Vagrant box and a defined database already installed) to include dev-level database creds. It should just work. If someone works on this site after me, they shouldn't have to spend a ton of time getting a config file set up.

Secondly, having a config.php file and a config-dev.php file frustrates me because for the developer that comes after me, they could really use the config-dev file! But if it's in the repo, it gets deployed. If it gets deployed, it gets called on the staging/production server, and that is no good! Also, config.php and config-dev.php would in most cases pretty much completely mirror one another with the exception of database creds, and maybe debug or a few other things.

That is why I want to have all the configs in the repo within config.php except for the tiny bits that are environment-specific. They should be able to be included, right?

  • Like 1
Link to comment
Share on other sites

7 minutes ago, ethanbeyer said:

require_once("./creds.php"); // this doesn't

Yep, it does not. Maybe because config.php is already included? I should check it :P Anyway, because of such issues I recently switched to using __DIR__, eg:

require_once __DIR__ . '/./creds.php';

 

Edited by szabesz
typo
  • 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

×
×
  • Create New...