2

I have AWS server where we are setting all system environment variables. For reference system environment variables like DATABASE connection, EMAIL CLIENT configurations and some other configuration.

We can read those system variables into PHP file as well as into terminal. But my requirement is to read system variable into .env file of laravel

Following works into PHP but not into .env file

env('VARIABLE_NAME')             
getenv('VARIABLE_NAME')
$_ENV['VARIABLE_NAME']

The reason for this is that we are setting system wide environment variable to avoid the secrets being stored in Physical files. And as a solution changing directly into module files each time is not practical solution. Is there any way by which we can Read/Set SYSTEM VARIABLE INTO .env file ??

Anyone any help would highly be appreciated. Thanks

enter image description here

Viral
  • 441
  • 9
  • 17
  • you don't need them into `.env` if they are environmental vars they should already be available to you without `.env`, `.env` is for when you don't have actual environmental vars set – lagbox Jan 18 '21 at 07:29
  • It's not recommended to do that since based on OS and distors they act differently and you can't rely on it. – AH.Pooladvand Jan 18 '21 at 07:40
  • Thank you for your comments. Actually system variable names are based on project names. That means "DB_DATABASE" -> Variable name is different among different projects. So there is no way for me to set same database variable for different projects. Similar for other variablels. – Viral Jan 18 '21 at 08:33
  • you can reference other variables in the `.env` file .... if you are on a recent version of Laravel you will see an example of that in the `.env` file already – lagbox Jan 18 '21 at 11:03

1 Answers1

2

There is no working way I found to use or read variable within .env file of laravel. However you can directly read system environment variables into config files of your laravel framework as given example below.

Set following and all other variables into your .htacess file and retrieve them back

hostname
DBname
DBusername
DBpassword
smtp_host
smtp_port
smtp_user
smtp_password
ssl_ca

Step 1 : Set those variables into .htaccess file

enter image description here

Step 2 : Use them into config/database.php

enter image description here

Step 3 : Use them into config/mail.php

enter image description here

I used this method for my laravel project as I don't find any solution for reading variables into .env file. Some says that following method works but it did not worked for me.

DB_DATABASE ${VARIABLE_NAME}
Viral
  • 1,329
  • 1
  • 9
  • 31