CodeIgniter Forums

Full Version: One .env File for Development and Production
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi There,

I have been using CI for years now and now trying my hands on CI4. It has a cool feature where environment variable can be set with .env file.

How can I use one env file for staging and production. I want the ability to change the environment and CI will pick up the relevant variables.

For example:



When the environment is changed, CI to pick up the respective credentials.

Please advise if this can be achieved in CI4

Environment files are intended to be specific to each environment. Non-secure configurations can be set in the Config files themselves. You could certainly reuse .env or parts of it but the intent is for each environment to have its own.
Try this:

1. have two files, envdev and envprod
2. use index.php to delete .env file
3. test for local or online platform
4. copy the relevant file to .env.
Looks like problem is that you don't fully understand how env file and logic works.

You have to repos / systems: staging and prod
Both has the "same" files commited through (or copied over - doesn't matter)

Both systems will use `.env` file - so this means that your own defined variables in those files MUST match (except ones that override system's config variables - that on your own)

Your own example should be like:


Thank you everyone for your feedback. It seems things that I want cannot be achieved.
It’s not that it could not be done, rather I think we are suggesting that you might be hammering a screw. It’s unclear to me what your use case is (whether you need a screwdriver or should find some nails) but if you have some compelling reason to proceed with your requested setup you likely won’t find resources in the framework or User Guide for it.
I set a bool “LOCALHOST” constant which may be useful:
define("LOCALHOST", "localhost"===$_SERVER["SERVER_ NAME"]):
Ci4 read. Env before any request 

See  exmple 

#sms. phoneNumber=123

getEnv('sms. PhoneNumber') ;