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.
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.
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.
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
Accepted answer not effective anymore after Laravel 5.2 version:
To get environment values in controllers or (any php file in laravel project) you have tow cases:
Case 1 (Predefined keys): If you need to get value from .env file which created by Laravel project as (default .env file), or by third party plugin you already installed it and follow the correct installation procedure of installation, See this answer for explain: https://stackoverflow.com/a/69707775/2877427
Case 2 (Not predefined keys - if you need generate new custom environment key): to do that follow these instructions:
Add your custom key in .env file, for example i will add this key:
UNIFONIC_APP_ID=xx2Erdasd7Ik7XfdesfaV9HX47
2. Add new php file in laravel_root/config folder, for example i will add this file config/custom.php and its contents like this:
return [
'unifonic_id' => env('UNIFONIC_APP_ID' , 'Here Default value will used of UNIFONIC_APP_ID not defined in .env file'),
];
Run these tow commands in terminal:
php artisan config:clear
php artisan config:cache
Now in any php file (Controllers, Models, Views, ... etc) you can call this function:
config('custom.unifonic_id');
Where custom is the name of php file which generated in step 2, and unifonic_id is the key of array which defined in step 2
This will return value if UNIFONIC_APP_ID which defined in .env, if not exists will return default value which defined in config file in step 2
Note 1: config file can return multi dimensions array, take look for
structers of config files which located in laravel_root/config
folder.
Note 2: you can modify file laravel_root/config/services.php insted
of create new config file...