Home » Php » Get environment value in controller

Get environment value in controller

Posted by: admin November 29, 2017 Leave a comment

Questions:

In my .env file I have the following:

IMAP_HOSTNAME_TEST=imap.gmail.com
[email protected]
IMAP_PASSWORD_TEST=mypw

Now I would like to use them in my controller. I’ve tried this but with no result:

$hostname = config('IMAP_HOSTNAME_TEST');

The $hostname variable is equal to null. How can I use these config variables in my controller?

Answers:

try it with

<?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>

Questions:
Answers:

All of the variables listed in .env file will be loaded into the $_ENV PHP super-global when your application receives a request.
Checkout laravel configuration page

$_ENV['yourkeyhere'];

Questions:
Answers:

May be not related, but it might help someone…. in Laravel just dd(config(‘app.env’));
and you’ll see ‘local’ or ‘production’