Laravel - custom .env file

laravel update env file programmatically
laravel env not working
laravel 5.8 env not working
laravel get all env variables
laravel get env variable in blade
how to access env file variable in laravel
laravel reload config
how to share env files

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 files for dev and production (say .env-production and .env-dev) in my repo and add some custom logic to which file should be used, for example based on current domain name.

Something like

if ($_SERVER['HTTP_HOST'] == 'prod.domain.com') {
    load('.env-production');
} else {
    load('.env-dev');
}

How can i achieve it? Thanks!

Nadeem0035 gave me pretty good idea what to do

bootstrap\app.php right before return $app;

$envFile = $_SERVER['HTTP_HOST'] == 'prod.domain.com' ? '.env-production' : '.env-dev';
$app->loadEnvironmentFrom($envFile);

Configuration - Laravel, At a high level, configuration files are located in the config/ folder of a can use to change values per-environment with the .env file or on the  Custom Laravel 101 Config. Make custom config file keys Make custom config files Test-specific .env.testing; Custom database migration type RefreshDatabase

Use Dotenv::load() for custom .env file

laravel 5.1 with vlucas/phpdotenv ~1.0

if ($_SERVER['HTTP_HOST'] == 'prod.domain.com') {
    Dotenv::load(__DIR__ . '/../','.production.env');
} else {
    Dotenv::load(__DIR__ . '/../','.dev.env');
}

OR

laravel 5.2 with vlucas/phpdotenv ~2.0

$dotenv = new Dotenv\Dotenv(__DIR__, 'myconfig'); // Laravel 5.2
$dotenv->load();

PHP dotenv

In bootstrap/app.php

Creating Your Own Configuration in Laravel, A common problem while working in a team is some custom So, in that .env.​example file you just put the key with empty value, like this: In a fresh Laravel installation, the root directory of your application will contain a.env.example file. If you install Laravel via Composer, this file will automatically be renamed to.env. Otherwise, you should rename the file manually.

I like to add a solution for people who have a shared codebase for many vhosts that all need different .env files for all the different things, like database connections, smtp settings etc.

For every vhost, on Apache, create a vhost config:

<VirtualHost *:80>
    ServerName your-vhost.yourdomain.com
    DocumentRoot /var/www/shared-codebase/public

    SetEnv VHOST_NAME 'your-vhost'

    <Directory "/var/www/shared-codebase/public">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride all
    Order deny,allow
    Require all granted
    </Directory>

    <IfModule mpm_itk_module>
       AssignUserId your-vhost your-vhost
    </IfModule>

    ErrorLog /var/www/your-vhost/logs/error.log
    CustomLog /var/www/your-vhost/logs/access.log combined
</VirtualHost>

All vhosts have the same document root and directory, because it is a shared codebase. Inside the config we added a SetEnv VHOST_NAME 'your-vhost' which we will later use in Laravel's bootstrap.php to change the location of vhost specific .env.

Next create the custom .env file in a folder(fe. /var/www/your-vhost/.env) the alter bootstrap.php so that it loads the .env from the right location.

<?php

$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

/*
|--------------------------------------------------------------------------
| Add the location of the custom env file
|--------------------------------------------------------------------------
*/    
$app->useEnvironmentPath('/var/www/'.$_SERVER['VHOST_NAME']);

return $app;

That's all.

[edit] I you want to target a specific database or want to generate a key for a specific .env, then you should put the VHOST_NAME in front of the artisan command.

VHOST_NAME=tenant2.domain.com php artisan key:generate

[edit] When working locally and are using Laravel Valet, then you can add a custom .valet-env.php in the root of your codebase. https://laravel.com/docs/master/valet#site-specific-environment-variables

How to add new .env variable, so teammates would , Notice: article was updated in Oct 2019, with latest Laravel 6 example. In your main Laravel folder you should have .env file which contains various Do you have any piece on how to setup custom env variables like the  A common problem while working in a team is some custom variables needed, when only one person on the team knows that they are needed, and then other people have errors because they don't have that variable. Like API Tokens for 3rd party apps, default values for some function - basically, anything that should be in .env file. So here's an instruction for you, how to put new environment

You've single .env file into laravel and you can define level of your app. APP_ENV=local OR APP_ENV=production You can set configuration as per your requirement and not need to create new .env file to here. More about Laravel Environment Variables : and Here's more descriptive help for you: phpdotenv

How to use Laravel .env and .env.example files? – Quick Admin Panel, Laravel Version: 5.5.37 PHP Version: 7.0.13 Database Driver & Version: MySql [ 5.6.34 ] Description: Hi Laravel, I want a functionality like  In the case of 'host', the value of MAIL_HOST in your custom config/.env file will be checked for an encrypted value. If it is encrypted, the value will be decrypted to be readable by Laravel's Config class. If it cannot find a encrypted value, it will assign the fallback value (smtp.somehost.com in the example).

Loading different env files using loadEnvironmentFrom is not , Accessing Custom Environment Variable. Posted 4 years ago by because of the dot? I would say keep the laravel convention in the .env file In your main Laravel folder you should have .env file which contains various settings, one row – one KEY=VALUE pair. And then, within your Laravel project code you can get those environment variables with function env(‘KEY’) .

Accessing Custom Environment Variable, Need a quick way to set an environment variable from the command line that'll save it to the Laravel .env file? Instead of two arguments split by  Laravel configuration allows you to define per-environment configuration with the excellent vlucas/phpdotenv package. If you are new to Laravel, you might not yet know how you can create your configuration files in your projects and a few other helpful things that will help you master configuration. There are a few things we&#8217;ll cover here that I think will help &hellip;

Laravel Set .env File Variable - Liam Hammett, Defining custom environment variables by just creating a .env.envname file? Cool! That is what custom-env has come to do. Custom env is a  In Laravel's Flysystem integration, "visibility" is an abstraction of file permissions across multiple platforms. Files may either be declared public or private. When a file is declared public, you are indicating that the file should generally be accessible to others.

How to customize Node.js .env files for different environment stages, If you would like to define an entirely custom channel in which you have full control over Monolog's instantiation and configuration, you may specify a custom driver type in your config/logging.php configuration file.

Comments
  • Thanks for answer, It did not work for me - since load is not a static method, also direct access to Dotenv may be not the best way, but you gave me good idea what to do and I come up with my answer - use $app->loadEnvironmentFrom($envFile); instead. Thanks!
  • @SmxCde my first code was tested in laravel 5.1.* with vlucas/phpdotenv ~1.0 link
  • I'd like to add that you could point to a different directory with $app->useEnvironmentPath('/var/www/'.$_SERVER['VHOST_NAME']); It solved my shared codebase, different vhost problem. Your VHOST config can then have a SetEnv <VirtualHost *:80> ServerName yoursub.domain.com DocumentRoot /var/www/yoursub/public SetEnv VHOST_NAME 'yoursub'
  • +1 for properly using $app->useEnvironmentPath(); for informing laravel of the new position of the .env file. Others solutions are overloading Dotenv\Dotenv and will bring issues if you and running artisan key:generate.
  • Thank you for the answer but this is not it: I still want to use .env files because it is convenient but I do not want all this bureaucracy and oversecurity that is unneeded when it is a small pet-project and I am the only maintainer. Schema proposed by default is good but too much effort for smaller projects.