Skip to content
Advertisement

How to use .env file in smarty?

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 :

JavaScript

My DSOPSettings.local.php :

JavaScript

I have also tried to get env variables as below but not working.

JavaScript

How can I make this working? Thanks in Advance!

Advertisement

Answer

I am searching a way to load env in smarty. I found it and implemented as follow.

.env

JavaScript

I have created one class to load all .env variables. Env.php

JavaScript

Load it in any file as follow :

JavaScript

Now use it with getenv() function. Ex. getenv('DB_WRITE_USER')

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement