Get environment value in controller

前端 未结 10 1607
既然无缘
既然无缘 2020-12-25 10:43

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

相关标签:
10条回答
  • 2020-12-25 11:21

    In config/app.php file make a instance of env variable like 'name' => env('APP_NAME', 'Laravel') & In your controller call it like config('app.name')

    Run following commands php artisan config:cache php artisan cache:clear if it is not working.

    0 讨论(0)
  • 2020-12-25 11:22

    You can use with this format (tested on Laravel 5.5). In my case I used it for gettting 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.

    Your .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
    
    0 讨论(0)
  • 2020-12-25 11:27

    In Controller

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

    In blade.view

    {{$_ENV['IMAP_HOSTNAME_TEST']}}
    
    0 讨论(0)
  • 2020-12-25 11:27

    You can not access the environment variable like this.

    Inside the .env file you write

    IMAP_HOSTNAME_TEST=imap.gmail.com  // I am okay with this
    

    Next, inside the config folder there is a file, mail.php. You may use this file to code. As you are working with mail functionality. You might use another file as well.

    return [
    
    //..... other declarations
    'imap_hostname_test' => env('IMAP_HOSTNAME_TEST'),
    // You are hiding the value inside the configuration as well
    
    ];
    

    You can call the variable from a controller using 'config(. Whatever file you are using inside config folder. You need to use that file name (without extension) + '.' + 'variable name' + ')'. In the current case you can call the variable as follows.

    $hostname = config('mail.imap_hostname_test');
    

    Since you declare the variable inside mail.php and the variable name is imap_hostname_test, you need to call it like this. If you declare this variable inside app.php then you should call

    $hostname = config('app.imap_hostname_test');
    
    0 讨论(0)
提交回复
热议问题