I'm upgrading Laravel to 5.2 from 5.1. When i refere to variable API_DOMAIN in .env file using $_ENV
$_ENV['API_DOMAIN']
I get an error saying Undefined index: API_DOMAIN". Am I missing something here? should i do something after composer update?
Asked
Active
Viewed 2,835 times
0

marmahan
- 183
- 2
- 15
-
According to the [upgrade guide](https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0) *If you are using the `config:cache` command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.* This also applies to using `$_ENV` since they both read from the same source – apokryfos May 11 '20 at 13:59
3 Answers
2
Run the below commands after making changes in env file. It will flush and repopulate all the env variable.
php artisan config:cache //flush all cached env variable
php artisan config:clear //repopulate all the env variable
php artisan cache:clear //flush all the cached content

Sakthivel A R
- 575
- 8
- 24
1
You should not directly work with environment values in your application, I would create a new configuration file or use an existing one and add the value there, this way you can cache the configuration.
Lets use your API_DOMAIN
as an example:
.env file
API_DOMAIN=www.mydomain.com
Config file
config/api.php
for example
<?php
return [
'domain' => env('API_DOMAIN'),
];
Now you can use the config helper to use this variable in your application:
$value = config('api.domain');
You should never use the env()
helper outside of config files, because when you cache your config, env()
will return null
.

Remul
- 7,874
- 1
- 13
- 30