1. Go to this page and download the library: Download cyberinferno/yii2-phpdotenv library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cyberinferno / yii2-phpdotenv example snippets
return [
//....
'bootstrap' => [
[
'class' => 'cyberinferno\yii\phpdotenv\Loader',
'path' => '@vendor/../', // Directory of the .env file
'file' => '.env', // Optional parameter if custom environment variable file
'overload' => false, // Optional parameter whether to overload already existing environment variables. Defaults to false
],
]
];
namespace common\components;
use cyberinferno\yii\phpdotenv\Loader;
use yii\helpers\ArrayHelper;
class PhpdotenvLoader extends Loader
{
public function bootstrap($app)
{
parent::bootstrap($app);
$app->setComponents(ArrayHelper::merge($app->getComponents(),
[
'db' => [
'class' => 'yii\db\Connection',
'dsn' => getenv('DB_DSN'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
],
]
));
}
}