Welcome Guest, Not a member yet? Register   Sign In
Access of env-vars in Constants.php
#1

(This post was last modified: 06-21-2022, 10:13 AM by groovebird.)

Hi,

i wanted to do something like that in the Config/Constants.php:
PHP Code:
define('IS_LIVE'getenv('CI_ENVIRONMENT') == 'production');
define('IS_DEV'getenv('CI_ENVIRONMENT') == 'development'); 

but i have no access to the env-variables. How could i do that? I want to keep this variables because in my old CI 3 installation i use this constants a lot and migration would be easier.

A second question: Where is the ENVIRONMENT constant defined? I found this term in the documentation. Is this the CI_ENVIRONMENT in the env file?
Reply
#2

If you set CI_ENVIRONMENT by .env file, it is not loaded yet when Config/Constants.php is loaded.

The ENVIRONMENT is defined in CodeIgniter::detectEnvironment().
It is not CI_ENVIRONMENT, but when ENVIRONMENT is not defined, CI_ENVIRONMENT value will be set.
Reply
#3

(This post was last modified: 06-21-2022, 11:38 PM by groovebird.)

Thank you.

Is there an alternative what i can do instead in order to keep these constants? Do i have to create something different and change the constants? (helper, files in Config/Boot Directory). It was nice if you can give me an idea, thanks.
Reply
#4

Move the define() to app/Config/Boot/*.php
Reply
#5

(06-22-2022, 03:16 AM)kenjis Wrote: Move the define() to app/Config/Boot/*.php

Thank you very much. I need to find the right places and implementations for different things :-)
Reply
#6

(This post was last modified: 5 hours ago by InsiteFX.)

I was having the same issue and I still can't get this to work.

App/Config/Boot/development.php

Code:
defined('ENVI') || define('ENVI', 'development');


App/Config/Boot/production.php

Code:
defined('ENVI') || define('ENVI', 'production');


App/Config/Constants.php

Code:
getenv('ENVI')

Doesn't have any value. It doesn't blow up with an error either.

Code:
ENVI

Blows up with an error

Code:
$_ENV['ENVI']

Blows up with an error
Reply
#7

@sevmusic I copied your code on a fresh install of CodeIgniter and added dd(ENVI); to the default Home Controller and it showed the appropriate value. You've done something else that isn't working correctly.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB