I am new to smarty template. My all database settings are in /inc/settings/DSOPSettings.local.php but Every time I get pull from git for changes It overrides.
I have tried to set .env file in smarty template project root tried to get env variables in DSOPSettings.local.php file but It's not working for me.
My .env file :
DB_WRITE_HOST=localhost
DB_WRITE_USER=root
DB_WRITE_PASS=test
DB_WRITE_DBASE_DEV=storytalk
My DSOPSettings.local.php :
define('DB_WRITE_HOST', env('DB_WRITE_HOST'));
define('DB_WRITE_USER', env('DB_WRITE_USER'));
define('DB_WRITE_PASS', env('DB_WRITE_PASS'));
define('DB_WRITE_DBASE_DEV', env('DB_WRITE_DBASE_DEV'));
I have also tried to get env variables as below but not working.
define('DB_WRITE_HOST', getenv('DB_WRITE_HOST'));
define('DB_WRITE_USER', getenv('DB_WRITE_USER'));
define('DB_WRITE_PASS', getenv('DB_WRITE_PASS')));
define('DB_WRITE_DBASE_DEV', getenv('DB_WRITE_DBASE_DEV'));
How can I make this working? Thanks in Advance!
I am searching a way to load env in smarty. I found it and implemented as follow.
.env
DB_WRITE_USER=root
DB_WRITE_PASS=test
DB_WRITE_DBASE_DEV=storytalk
I have created one class to load all .env variables. Env.php
<?php
namespace EnvironmentSettings;
class Env
{
public function __construct()
{
}
public function load($path)
{
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
}
if (!is_readable($path)) {
throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value);
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
}
}
return true;
}
}
Load it in any file as follow :
$env = new EnvironmentSettings\Env();
$env->load(INSTALLED_ROOT_PATH.INSTALLED_PATH."/.env");
Now use it with getenv() function. Ex. getenv('DB_WRITE_USER')