Get environment value in controller

2019-03-12 02:10发布

In my .env file I have the following:

IMAP_HOSTNAME_TEST=imap.gmail.com
IMAP_USERNAME_TEST=myemail@gmail.com
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?

9条回答
beautiful°
2楼-- · 2019-03-12 02:45

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'];
查看更多
家丑人穷心不美
3楼-- · 2019-03-12 02:50

InController

$hostname = $_ENV['IMAP_HOSTNAME_TEST']; (or) $hostname = env('IMAP_HOSTNAME_TEST');

In blade.view

{{$_ENV['IMAP_HOSTNAME_TEST']}}
查看更多
走好不送
4楼-- · 2019-03-12 02:54

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

查看更多
登录 后发表回答