Get environment value in controller
Solution 1
Try it with:
<?php $hostname = env("IMAP_HOSTNAME_TEST", "somedefaultvalue"); ?>
Solution 2
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".
Solution 3
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.
Solution 4
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'];
Solution 5
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
nielsv
Updated on October 26, 2021Comments
-
nielsv over 2 years
In my .env file I have the following:
IMAP_HOSTNAME_TEST=imap.gmail.com [email protected] IMAP_PASSWORD_TEST=mypw
Now I would like to use them in my controller. I've tried this, but without any result:
$hostname = config('IMAP_HOSTNAME_TEST');
The $hostname variable is equal to null. How can I use these configuration variables in my controller?
-
Chetan Ameta over 8 years
somedefaultvalue
can be any default value in caseenv
does not exists. -
Drew Hammond over 6 yearsNot sure why this was downvoted... It's the correct answer (mostly) for L5.3+. But using your example filename of
values.php
, you'd actually reference it in the controller as$value = config('values.myvalue');
-
Grant about 6 yearsDon't forget to
php artisan config:cache
to commit config changes. -
Wouter Vanherck over 5 yearsWhere did you place this code,
config/app.php
or the controller/view? This worked for me onLaravel 5.2
but doesn't seem to onLaravel 5.3+
. Since you're specifyingLaravel 5.5
, I must be doing something wrong? -
Steve over 5 yearsUsing
env()
can lead to unexpected issues, see Grant's answer on this question - stackoverflow.com/questions/34263107/…. -
Zarkys Salas about 5 yearsi have placed this code on app/Http/ExampleController.php
-
Felipe Castillo about 5 yearsphp artisan config:cache crash my aplication
-
Grant about 5 years@FelipeCastillo means your application has an issue in its configuration - because that command only flushes the cache
-
Hari Harker almost 5 yearsDoesn't work for Laravel 5.5. Please go down to find an answer by Masum Ahmed Sarkar
-
Karna over 4 yearsexactly what I was looking for
-
james ace almost 4 yearsThis is the best and simple answer
-
Ametad almost 4 yearsconfig() is the helper function and does NOT need the
use Config;
. With the use statement you can use the aliasConfig
forIlluminate\Support\Facades\Config::class
like for example:Config::get('example.url')
-
CodeConnoisseur over 3 years@petermortensen @Grant, this is a great answer. My question is why does this work as opposed to just using laravels
env()
helper method? -
Grant over 3 years@PA-GW The env() helper accesses only the (often security sensitive) environment variables stored in the .env file. You'll notice in my opening line, I mentioned that it appears that (at least as of the date answering this question) configuration files are the only files that are able to access those sensitive super globals.
-
Steven over 3 yearsAdd a reducted form of this in the config/app.php file in Laravel 8^ and it will work.
'hostname' = env('IMAP_HOSTNAME_TEST');
-
endo64 almost 3 yearsThis is the correct and the best answer on how to get values from
.env
file. -
Ducky almost 3 yearsThis is the actual right answer to the OP's question