可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
try it with
<?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>
回答2:
Doesn't work in Laravel 5.3+ if you want to try to access the value from the controller like below, it returns always null
<?php
$value=env('MY_VALUE','default_value');
SOLUTION: Rather you need to create a file in config folder ..say values.php and then
write the codes like below
values.php
<?php
return [
'myvalue' => env('MY_VALUE',null),
//add other values as you want
]
Then access the value in your controller with the following code
<?php
$value=\Config::get('credentials.myvalue')
Where "credentials" is the filename followed by the key "myvalue"
Hope it helps
回答3:
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'];
回答4:
I thought I'd add an answer, to simplify what has been said in the past. Only config files can access env variables - and then pass them on.
Step 1.) Add your variable to your .env
file, ie.
EXAMPLE_URL="http://google.com"
Step 2.) Create a new file inside of the config
folder, any name, ie.
config/example.php
Step 3.) Inside of this new file, I add an array being returned, containing that env variable.
<?php
return [
'url' => env('EXAMPLE_URL')
];
Step 4.) Because I named it "example" my configuration 'namespace' is now example. So now, in my controller I can access this variable with:
$url = \config('example.url');
Tip - if you add use Config;
at the top of your controller, you don't need the backslash (which designates the root namespace). Ie.
namespace App\Http\Controllers;
use Config; // Added this line
class ExampleController extends Controller
{
public function url() {
return config('example.url');
}
}
--- IMPORTANT --- Remember to enter into the console php artisan config:cache
once you have created your example.php file. Config files and variables are cached, so if you make changes you need to flush that cache - the same applies to the .env
file being changed / added to.
回答5:
You can use with this format, (Tested on Laravel 5.5) in my case i used for get the data of database connections and use on Controller:
$User = env('DB_USERNAMEchild','');
$Pass = env('DB_PASSWORDchild','');
The second parameter can be null, or set any default in case of DB_USERNAMEchild is null.
you .env file can be the same:
DB_HOST=localhost
DB_DATABASE=FATHERBD
DB_USERNAME=root
DB_PASSWORD=password
DB_DATABASEchild=ZTEST
DB_USERNAMEchild=root
DB_PASSWORDchild=passwordofchild
回答6:
InController
$hostname = $_ENV['IMAP_HOSTNAME_TEST']; (or) $hostname = env('IMAP_HOSTNAME_TEST');
In blade.view
{{$_ENV['IMAP_HOSTNAME_TEST']}}
回答7:
in Laravel Controlller
public static function mail($param)
{
//$_ENV['yourkeyhere'];
$mailgunsecret = env('MAILGUN_SECRET');
$mailguurl = env('MAILGUN_DOMAIN');
}
in Laravel Blade files
@if (env('APP_ENV')!='Production')
Enviroment Test
@endif
must run this commands
php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear
Example : Accessing Laravel .env variables in blade
APP_ENV=local
APP_KEY=////
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
APP_GOOGLE_MAPS=////
APP_OVERHEID_IO=////
{{ env('APP_ENV') }} // returns 'local'
{{ env('APP_URL') }} // returns 'http://localhost'
Get environment value in controller
回答8:
It's a better idea to put your config variables in a config file.
In your case, I would suggest putting your variables in config/mail.php like
'imap_hostname' => env('IMAP_HOSTNAME_TEST', 'imap.gmail.com')
and refer to them by
config('mail.imap_hostname')
It first tries to get config variable value in the .env file and if it couldn't find the variable value in the .env file it will get variable value from config/mail.php
回答9:
May be not related, but it might help someone.... in Laravel just dd(config('app.env'));
and you'll see 'local' or 'production'