Giter Site home page Giter Site logo

Comments (9)

reinink avatar reinink commented on May 2, 2024 2

I actually see this being helpful as well, and I'm curious to get @vlucas thoughts on this.

I use phpdotenv in my development environement to set configuration settings. However, in some production environments these variables are already set another way, and the .env isn't needed. For example, Heroku allows you to specify environment variables via their control panel.

Naturally, the easy way to implement this yourself is with a check:

if (is_file(__DIR__ . '/.env')) {
    Dotenv::load(__DIR__);
}

from phpdotenv.

kamazee avatar kamazee commented on May 2, 2024

@reinink yep, that's exactly what I did as a workaround. I would discourage doing so, though: client code is not supposed to know anything about where environment variables are actually written, especially in the light of possible support for multiple backends

from phpdotenv.

vlucas avatar vlucas commented on May 2, 2024

phpdotenv is not meant to be used in production - it is meant to load environment variables in development, so you can simulate production without going through the hassle. I personally do this:

// Load required environment variables from .env in development
if(BULLET_ENV == 'development') {
    Dotenv::load(dirname(__DIR__));
}

// Required ENV vars for this app in production
Dotenv::required([
    'DATABASE_URL',
    'GOOGLE_PLACES_API_KEY',
    // ...
]);

So I still include the phpdotenv library in production to check required ENV variables, but don't load or parse a .env file in production, as this would happen on each request, and would impact perofrmance.

from phpdotenv.

kamazee avatar kamazee commented on May 2, 2024

@vlucas where do you keep DATABASE_URL, GOOGLE_PLACES_API_KEY etc themselves?

Frankly speaking, I don't think parsing a small text file can possibly impact performance that badly (even if it's going to be the only file that is loaded/parsed in a real-life project, it's very likely to get into OS cache, so it won't even cause IO; parsing overhead is really negligible).
On the other hand, it simplifies configuring application by introducing a configuration storage that is untied from both the application itself and the entry point (e.g. database credentials can be written only once for all entry points -- e.g. web and CLI -- and outside of the application which makes deployments easier to maintain and less error prone).

from phpdotenv.

kamazee avatar kamazee commented on May 2, 2024

@vlucas so, rephrasing my previous comment: I think phpdotenv is great in its simplicity and it totally can be used in production because it simplifies some important things while introducing just a tiny overhead.

from phpdotenv.

vlucas avatar vlucas commented on May 2, 2024

I do agree that it can certainly simplify things on production, especially if you're not using a cloud provider. On Heroku and other cloud providers though, you just set the ENV variables once, and any server or CLI command you run after that will have those ENV vars loaded. That's why I don't use phpdotenv to load a .env file in production personally.

from phpdotenv.

reinink avatar reinink commented on May 2, 2024

Vance, thanks for your input. One question, where is your BULLET_ENV variable being set?

if(BULLET_ENV == 'development') {
    Dotenv::load(dirname(__DIR__));
}

from phpdotenv.

vlucas avatar vlucas commented on May 2, 2024

I define it a few lines above that in the main index.php that bootstraps the whole application.

define('BULLET_ENV', $request->env('BULLET_ENV', 'development'));

It's from an expected environment variable, with a default to 'development' if not set. It's called BULLET_ENV because I use my own Bullet Micro-Framework.

from phpdotenv.

reinink avatar reinink commented on May 2, 2024

It's from an expected environment variable, with a default to 'development' if not set.

Gotcha.

from phpdotenv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.