Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ DB_NAME=database_name
DB_USER=database_user
DB_PASSWORD=database_password

# Or you can use a DSN (database source name)
# In this case you can leave empty variables DB_NAME, DB_USER, DB_PASSWORD, DB_HOST
# DATABASE_URL=mysql://database_user:database_password@database_host:database_port/database_name

# Optional variables
# DB_HOST=localhost
# DB_PREFIX=wp_
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ Much of the philosophy behind Bedrock is inspired by the [Twelve-Factor App](htt
$ composer create-project roots/bedrock
```
2. Update environment variables in the `.env` file:
* `DB_NAME` - Database name
* `DB_USER` - Database user
* `DB_PASSWORD` - Database password
* `DB_HOST` - Database host
* Database variables
* `DB_NAME` - Database name
* `DB_USER` - Database user
* `DB_PASSWORD` - Database password
* `DB_HOST` - Database host
* You can also use a variable `DATABASE_URL` instead of using all the database variables above, that contains a DSN (ex: `mysql://user:password@127.0.0.1:3306/db_name`)
* `WP_ENV` - Set to environment (`development`, `staging`, `production`)
* `WP_HOME` - Full URL to WordPress home (https://example.com)
* `WP_SITEURL` - Full URL to WordPress including subdirectory (https://example.com/wp)
Expand Down
16 changes: 15 additions & 1 deletion config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
$dotenv = new Dotenv\Dotenv($root_dir);
if (file_exists($root_dir . '/.env')) {
$dotenv->load();
$dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD', 'WP_HOME', 'WP_SITEURL']);
$dotenv->required(['WP_HOME', 'WP_SITEURL']);
if (!env('DATABASE_URL')) {
$dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD']);
}
}

/**
Expand Down Expand Up @@ -60,6 +63,17 @@
Config::define('DB_COLLATE', '');
$table_prefix = env('DB_PREFIX') ?: 'wp_';

if (env('DATABASE_URL')) {
Comment thread
swalkinshaw marked this conversation as resolved.
$database_dsn = parse_url(env('DATABASE_URL'));
$db_name = substr($database_dsn['path'], 1);
$db_host = isset($database_dsn['port']) ? $database_dsn['host'] . ":" . $database_dsn['port'] : $database_dsn['host'];

Config::define('DB_NAME', $db_name);
Config::define('DB_USER', $database_dsn['user']);
Config::define('DB_PASSWORD', $database_dsn['pass'] ?? null);
Config::define('DB_HOST', $db_host);
}

/**
* Authentication Unique Keys and Salts
*/
Expand Down