CodeIgniter Forums
Handling environments using .env - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Handling environments using .env (/showthread.php?tid=71831)



Handling environments using .env - skunkbad - 09-29-2018

I started with an .htaccess that was pretty standard:


Code:
RewriteEngine On
RewriteBase /

RewriteRule ^(system|application|cgi-bin) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]


I know CI4 comes with its own .htaccess, but they can sometimes be finicky, so I thought I'd test out using a different one.

Per documentation here: https://bcit-ci.github.io/CodeIgniter4/general/environments.html#env

Seems like I should be able to add this to my .env:


Code:
CI_ENVIRONMENT = development


and then CodeIgniter should start displaying errors instead of "whoops". This does not work for me. The only way I can change the environment to development is with this line in .htaccess:


Code:
SetEnv CI_ENVIRONMENT development


So, then I noticed that the env file in root directory is not prefixed with a dot. I added the dot, but still no love.

After that I swapped out my .htaccess file for the one provided by CI4, but still not love.

On this page https://bcit-ci.github.io/CodeIgniter4/general/configuration.html it seems to indicate that nothing else should need to be done:


Quote:When your application runs, this file will be automatically loaded and the variables will be put into the environment. This will work in any environment.

If it matters, this is a Ubuntu 16.04 / PHP 7.2 server.

What does one do to get the .env file to work? Please point to documentation if available.


RE: Handling environments using .env - sv3tli0 - 09-29-2018

By default you receive .htaccess file inside the public folder which has
Code:
SetEnv CI_ENVIRONMENT development

If you comment it with # then the value from .env will be used.

To be honest I think that it is not logical .env to be after htaccess but as .env is per instance while the .htaccess is usually stored inside the git repo...


RE: Handling environments using .env - skunkbad - 09-29-2018

(09-29-2018, 06:55 AM)sv3tli0 Wrote: By default you receive .htaccess file inside the public folder which has
Code:
SetEnv CI_ENVIRONMENT development

If you comment it with # then the value from .env will be used.

To be honest I think that it is not logical .env to be after htaccess but as .env is per instance while the .htaccess is usually stored inside the git repo...

This is what I expect, but is not working.


RE: Handling environments using .env - sv3tli0 - 09-29-2018

(09-29-2018, 07:18 AM)skunkbad Wrote:
(09-29-2018, 06:55 AM)sv3tli0 Wrote: By default you receive .htaccess file inside the public folder which has
Code:
SetEnv CI_ENVIRONMENT development

If you comment it with # then the value from .env will be used.

To be honest I think that it is not logical .env to be after htaccess but as .env is per instance while the .htaccess is usually stored inside the git repo...

This is what I expect, but is not working.


CI_ENVIRONMENT=production 

And at the first page it shows production Smile


RE: Handling environments using .env - happyape - 09-29-2018

Yes I got confused with the same i.e. I tried to set the CI_ENVIRONMENT in .env file but it didn't work because I didn't realise it was being set in the public/.htaccess. As mentioned above, I simply commented that out and it works fine now. I suppose it should be made clearer in the documentation.


RE: Handling environments using .env - sv3tli0 - 09-29-2018

The only thing that may be wrong at this case is that we have by default Setenv in the .htaccess ..

By default it should be set inside the env file to development .


RE: Handling environments using .env - danielsonsilva - 04-30-2020

I can't make it work for me too.
I didn't understand the solution to change to development mode.
I opened the env file and changed to:
Code:
#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

# CI_ENVIRONMENT = production
CI_ENVIRONMENT = development

But when error occurs, it shows me the Whoops message.
The documentation says that the only change that we have to do is in .env file...

And I couldn't find no SetEnv on the .htaccess files, inside public and inside app.


RE: Handling environments using .env - jreklund - 04-30-2020

You need to make a copy of your "env" file and name it ".env". The . (dot) are required.

PS. This thread are from 2018, a lot have changed since then. So it may not be in the file itself, but you can add it to .htaccess.