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条回答
贼婆χ
2楼-- · 2019-03-12 02:30

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

查看更多
地球回转人心会变
3楼-- · 2019-03-12 02:32

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

查看更多
何必那么认真
4楼-- · 2019-03-12 02:39

try it with

<?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>
查看更多
Emotional °昔
5楼-- · 2019-03-12 02:39

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

查看更多
Animai°情兽
6楼-- · 2019-03-12 02:41

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
查看更多
在下西门庆
7楼-- · 2019-03-12 02:43

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.

查看更多
登录 后发表回答