Laravel - custom .env file

前端 未结 6 1856
难免孤独
难免孤独 2020-12-31 19:41

Laravel assumes that .env file should describe environment, and it should not be committed to your repo.

What if I want to keep both .env f

6条回答
  •  离开以前
    2020-12-31 20:06

    I'd like to share my two cents on this, for team working on different machines / hosts. I create a directory env on app's root, containing:

    • a .master.env file with the main and shared configuration.
    • a .name file containing only a string with the environment's name for specific machine / purpose (e.g. "server1")
    • the specific .env file matching the name defined above, e.g. .server1.env.

    Then, in bootstrap/app.php:

    /**
     * master config
     */
    $app->useEnvironmentPath(__DIR__.'/../env');
    $app->loadEnvironmentFrom('.master.env');
    
    /**
     * config overloading
     */
    $app->afterLoadingEnvironment(function() use($app) {
        $envFile = trim(file_get_contents($app->environmentPath().'/.name'));
        if ($envFile && file_exists($app->environmentPath().'/.' .$envFile .'.env')) {
            $dotenv = Dotenv\Dotenv::create($app->environmentPath(), '.'.$envFile.'.env');
            $dotenv->overload();
        }
    });
    

    Now you can selectively override configuration keys for specific machines and if you don't have security issues you can put the env files in VCS , as long as you ignore the `.name' file.

    Working in Laravel 5.8.

提交回复
热议问题