goatshark's avatar

.env vs config files

Hello there,

I'm trying to decide whether or not to keep configuration/environment variables in .env or in a config/ file. Where do you like to keep them? What are the pros and cons to each? As far as I can tell, I suspect that a config file is preferred because config file options can be cached with config:cache.

Either way, it seems that you would be configuring whichever one you choose for each deployment, so ease of deployment doesn't seem to be the driver for the choice.

Thoughts? Am I missing something here?

0 likes
6 replies
tomicakorac's avatar

For me it depends on what kind of variables are in question. .env file is extremely useful for people using git to collaborate on the same project. It increases security when work is shared.

Actually, there aren't that many situations when config and .env overlap. If some value is identical in dev and production environments, and/or it's safe to reveal it to your collaborators, then it goes to a config file. Otherwise, it's a .env variable.

1 like
frezno's avatar

the .env file makes sense esp. for different settings of your development server and the production sever.
ie the Environment Configuration. Those data is 'outsourced'. (ok, that's not 100% correct but helps thinking about it)

the config files on the other hand are variables for specific tasks which are not server specific, eg

// config/blog.php
<?php
return [
    'name' => "Freznos BlogPage",
    'title' => "Freznos Motocross Blog",
    'subtitle' => 'A Blog for Motocross Enthusiasts ',
    'description' => 'This blog is all about the motocross sport',
    'posts_per_page' => 10,
    ],
];

and now using it within your files, eg:

// within views:
<meta name="description" content="{!! $meta_description !!}">
<title>{{ $title or config('blog.title') }}</title>
//
@extends('blog.layout', [
  'title' => $post->title,
  'meta_description' => $post->meta_description ?: config('blog.description'),
])
//
// using controller:
$posts = Post::with('tags')
            ->where('published_at', '<=', Carbon::now())
            ->orderBy('published_at', 'desc')
            ->simplePaginate(config('blog.posts_per_page')); 

would (and could) you put those specific data into the .env file?
and even if you would, just imagine, you have eg different pages_per_post for pagingination, depending on whether its you blog or comments or shop or admin area etc.

goatshark's avatar

@tomicakorac, @frezno Thanks for your input. The variables that made me ask are directory paths that are different between my dev environment and production, and I imagine will differ for anyone installing this application. It sounds like .env is where I'll want them. Thank you.

frezno's avatar

It sounds like .env is where I'll want them

right, @goatshark , in this case i'd add them to the .env (as well)

pmall's avatar

Put values in config files for application configuration, which does not change whatever the server it is running on. Use env files for values which depends of the server. Canonical example : database password.

Also, put everything you dont want to be sored with git in the env file.

matthewbauer's avatar

Where would S3 configuration end up going though? If it's something that's specific like my Bucket, my region, a secret key and all of that it's not gonna sit on env is it? It would go to a config file right?

Please or to participate in this conversation.