Search code examples
laravelenvironment-variablessystem-variable

In LARAVEL .ENV file read SYSTEM VARIABLE


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


Solution

  • 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}