marmahan
marmahan

Reputation: 181

Laravel after upgrade, .env variable Undefined index

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?

Upvotes: 0

Views: 3402

Answers (3)

Arif Pavel
Arif Pavel

Reputation: 93

Try using the env helper instead, env('API_DOMAIN')

Upvotes: 2

Sakthivel A R
Sakthivel A R

Reputation: 585

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

Upvotes: 2

Remul
Remul

Reputation: 8242

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.

Upvotes: 1

Related Questions