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
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.
As @Rajib pointed out, You can't access your env variables using config('myVariable')
config
directory. I usually add to config/app.php
Config::get('fileWhichContainsVariable.Variable');
using the Config
FacadeProbably You will have to clear config cache using php artisan config:clear
AND
you will also have to restart server.
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.
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'];
Try it with:
<?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>
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
<?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".