Get environment value in controller

前端 未结 10 1606
既然无缘
既然无缘 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:02

    To simplify: Only configuration files can access environment variables - and then pass them on.

    Step 1.) Add your variable to your .env file, for example,

    EXAMPLE_URL="http://google.com"
    

    Step 2.) Create a new file inside of the config folder, with any name, for example,

    config/example.php
    

    Step 3.) Inside of this new file, I add an array being returned, containing that environment 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). For example,

    namespace App\Http\Controllers;
    use Config; // Added this line
    
    class ExampleController extends Controller
    {
        public function url() {
            return config('example.url');
        }
    }
    

    Finally, commit the changes:

    php artisan config:cache
    

    --- IMPORTANT --- Remember to enter php artisan config:cache into the console once you have created your example.php file. Configuration 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.

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

    As @Rajib pointed out, You can't access your env variables using config('myVariable')

    1. You have to add the variable to .env file first.
    2. Add the variable to some config file in config directory. I usually add to config/app.php
    3. Once done, will access them like Config::get('fileWhichContainsVariable.Variable'); using the Config Facade

    Probably You will have to clear config cache using php artisan config:clear AND you will also have to restart server.

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

    It's a better idea to put your configuration variables in a configuration 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 the configuration variable value in the .env file and if it couldn't find the variable value in the .env file, it will get the variable value from file config/mail.php.

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

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

    $_ENV['yourkeyhere'];
    
    0 讨论(0)
  • 2020-12-25 11:13

    Try it with:

    <?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>
    
    0 讨论(0)
  • 2020-12-25 11:21

    It Doesn't work in Laravel 5.3+ if you want to try to access the value from the controller like below. It always returns null

    <?php
    
        $value = env('MY_VALUE', 'default_value');
    

    SOLUTION: Rather, you need to create a file in the configuration folder, say values.php and then write the code like below

    File values.php

    <?php
    
        return [
    
            'myvalue' => env('MY_VALUE',null),
    
            // Add other values as you wish
    

    Then access the value in your controller with the following code

    <?php
    
        $value = \Config::get('values.myvalue')
    

    Where "values" is the filename followed by the key "myvalue".

    0 讨论(0)
提交回复
热议问题