From 242ef4bfdec77fadb029871d2339b4bd0ded790d Mon Sep 17 00:00:00 2001 From: Karthik P Date: Thu, 29 Jun 2017 13:46:56 +0530 Subject: [PATCH] Updated README with code example --- README.md | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7d40af3..84d044b 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,48 @@ return [ ] ]; ``` -Accessing ``Dotenv`` object: +To use components which will access environment variables extend Loader class like this: ```php -$dotenv = \Yii::$app->dotenv->dotenv; +setComponents(ArrayHelper::merge($app->getComponents(), + [ + 'db' => [ + 'class' => 'yii\db\Connection', + 'dsn' => getenv('DB_DSN'), + 'username' => getenv('DB_USERNAME'), + 'password' => getenv('DB_PASSWORD'), + 'charset' => 'utf8', + ], + ] + )); + } +} +``` + +Bootstrap this class in your config like this: + +```php +return [ + //.... + 'bootstrap' => [ + [ + 'class' => 'common\components\PhpdotenvLoader' + ], + ] +]; ``` -For further information about methods available in ``Dotenv`` class refer [Phpdotevn README](https://github.com/vlucas/phpdotenv/blob/master/README.md) \ No newline at end of file +This extension was tested to be working well with [Yii2 Advanced Template](https://github.com/yiisoft/yii2-app-advanced) +but can be used in any Yii2 application by sending proper .env file path while bootstrapping the extension. \ No newline at end of file