Get environment value in controller

199,874

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
Share:
199,874
nielsv
Author by

nielsv

Updated on October 26, 2021

Comments

  • nielsv
    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
    Chetan Ameta over 8 years
    somedefaultvalue can be any default value in case env does not exists.
  • Drew Hammond
    Drew Hammond over 6 years
    Not 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
    Grant about 6 years
    Don't forget to php artisan config:cache to commit config changes.
  • Wouter Vanherck
    Wouter Vanherck over 5 years
    Where did you place this code, config/app.php or the controller/view? This worked for me on Laravel 5.2 but doesn't seem to on Laravel 5.3+. Since you're specifying Laravel 5.5, I must be doing something wrong?
  • Steve
    Steve over 5 years
    Using env() can lead to unexpected issues, see Grant's answer on this question - stackoverflow.com/questions/34263107/….
  • Zarkys Salas
    Zarkys Salas about 5 years
    i have placed this code on app/Http/ExampleController.php
  • Felipe Castillo
    Felipe Castillo about 5 years
    php artisan config:cache crash my aplication
  • Grant
    Grant about 5 years
    @FelipeCastillo means your application has an issue in its configuration - because that command only flushes the cache
  • Hari Harker
    Hari Harker almost 5 years
    Doesn't work for Laravel 5.5. Please go down to find an answer by Masum Ahmed Sarkar
  • Karna
    Karna over 4 years
    exactly what I was looking for
  • james ace
    james ace almost 4 years
    This is the best and simple answer
  • Ametad
    Ametad almost 4 years
    config() is the helper function and does NOT need the use Config;. With the use statement you can use the alias Config for Illuminate\Support\Facades\Config::class like for example: Config::get('example.url')
  • CodeConnoisseur
    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
    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
    Steven over 3 years
    Add a reducted form of this in the config/app.php file in Laravel 8^ and it will work. 'hostname' = env('IMAP_HOSTNAME_TEST');
  • endo64
    endo64 almost 3 years
    This is the correct and the best answer on how to get values from .env file.
  • Ducky
    Ducky almost 3 years
    This is the actual right answer to the OP's question