Roots Discourse

How to load .env variable?

First, I’m not using Bedrock or Trellis. It seems like all the answers I come across are for those frameworks.

I added a .env file to the root of my Sage project, but also tried adding the .env file on the same level as wp-config. Inside, I defined DEV_MAP_KEY=xxxxxxxxxxblahxxxxxxxxx.

In wp-config, I added define('DEV_MAP_KEY', getenv('DEV_MAP_KEY'));
I don’t know if that’s entirely necessary.

In my setup.php file, I have a function which works if I hard-code the key value, so I know it’s working, but if I use getenv, it’s not:

<script src="//maps.googleapis.com/maps/api/js?key=<?php echo getenv('DEV_MAP_KEY'); ?>">
</script>

What am I doing wrong?

Sage doesn’t load env files, Bedrock does that. If you want to load env files in Sage you would need to build your own system for that.

Thanks @alwaysblank.

thanks for the awesome information.