vincej's avatar
Level 15

Understanding Env. Variables

I Have watched Jeffrey's lesson on this as well as read the L5 docs and read Matt Stauffers guide on upgrading and still. I have a general idea how .env works but the specifics are not clear to me:

  1. In my .env file I set up 1 and only 1 environment eg "local" or "production" - correct?

  2. In config/database.php I the second value in the below array is "forge" - but I am not using forge, so what kind of fall back would I put in there ?

'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],
  1. In matt stauffers guide to upgrading, he says:

Now, you can edit your APP_ENV--which, as you can tell from the default, is the primary way for us to set the application environment name. Check out the newer, simpler environment detection in bootstrap/environment.php:

Where do I find " APP_ENV" ? Also, there is no file bootstrap/environment.php.

Then he goes on to reference:

$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'production';
});

Sorry if these are dumb questions, but I have tried to find the answers for myself, without much luck.

Many thanks !!

0 likes
8 replies
RomainLanz's avatar

Hi,

  1. Yes.
  2. The second argument is only use if you don't set the environment variable.
  3. You need to set this environment variable on your .env file.
vincej's avatar
Level 15

Ok - I'm starting to get it, but i still don't get the Matt Stauffer thing. What does this code actually do ? why am I using it ?

What does Matt mean by bootstrap/environment.php ?

Am I supposed to this:

APP_ENV=local
APP_DEBUG=true
APP_KEY=Z1GsXPpJxJYR1sL5IKvBFnIbiH6dG7qY

DB_HOST=localhost
DB_DATABASE= mydb
DB_USERNAME=myusername
DB_PASSWORD=mypassword

CACHE_DRIVER=file
SESSION_DRIVER=file



$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'local';
});

vincej's avatar
Level 15

Ok - I kinda got what ENV vars are, but that Matt thing really through me. So, summing up, I ignore the Matt piece and I just put my env variables into .env and be happy !

You are a mega star thank you !

bashy's avatar

There are ways to change environment depending on something but bootstrap/environment.php isn't the way. I've seen a few threads on here about it but I don't think you need anything advanced? Just want to set it to local right?

vincej's avatar
Level 15

yup - local for now, and then later, production when it is uploaded.

michaeldyrynda's avatar

Depending on where you upload, you won't even have the .env file in production. It is merely a work around to keep you having to constantly bounce nginx/apache when you are adding new environment variables whilst developing.

If you're using Forge, you'll set the environment variables via the Forge interface and not have a .env file at all - just set the keys and values there.

The environment detection is now done within Illuminate/Foundation/Bootstrap/DetectEnvironment.php and basically loads your environment variables, then uses the APP_ENV value if it's set, defaulting to production.

Please or to participate in this conversation.