0

After upgrade from 5.0 to 5.8, laravel stopped reading env variables if the env is different than .env file.

For example, if i have .env file with the USE_SSL=true inside it. env('USE_SSL') will be true

But if i have .env file pointing to another env:

APP_ENV=dev

and than i'll have .env.dev file containing USE_SSL=true , env('USE_SSL') will be null.

  1. I tried composer dump-autoload and php artisan config:clear, and php artisan config:cache - no luck. cached or not, i can't get the value.
  2. I tried naming the files .dev.env and .env.dev - no luck.

Any ideas would be appreciated.

jvrnt
  • 635
  • 1
  • 6
  • 20
  • 1
    Once the configuration has been cached, the `.env` file will not be loaded and all calls to the `env` function will return `null` : see in [doc](https://laravel.com/docs/5.8/configuration#configuration-caching). – jcheron Jul 18 '19 at 14:37
  • @jcHache i've added the value and than cached, but i added it in an edit to prevent confusion. – jvrnt Jul 18 '19 at 14:40
  • Another part of the same documentation specifies: If you execute the `config:cache` command during your deployment process, you should be sure that you are only calling the `env` function from within your configuration files. From where do you make a call to `env`? – jcheron Jul 18 '19 at 14:49
  • I don't execute it in deployment – jvrnt Jul 18 '19 at 14:50
  • And where is your `env` call? – jcheron Jul 18 '19 at 15:01
  • 1
    I don't think this sort of chained `.env` approach works, as it's not really how you're supposed to use `.env` anyways. – ceejayoz Jul 18 '19 at 15:23
  • And you don't have your answer here? https://stackoverflow.com/questions/34420761/laravel-5-2-not-reading-env-file?rq=1 – jcheron Jul 18 '19 at 15:35

2 Answers2

1

I'm adding this as an answer, but please note this isn't how .env file should be used according to laravel docs. It's just a way i needed to use due to some restrictions which required me to use different config files for each env, and load it in runtime. For correct usage of .env file check the docs.

This is a way of loading different config files in runtime depends on where the APP_ENV is pointing. I'm marking this as answered since it's answering this specific question.

.env

APP_ENV=specific_domain

.env.specific_domain

USE_SSL=true

Http/Kernel.php

public function __construct(Application $app, Router $router)
{
    parent::__construct($app, $router);

    $app_env = explode("=", file($app->environmentFilePath(), FILE_IGNORE_NEW_LINES)[0])[1];
    $app->loadEnvironmentFrom(".env.$app_env");

}
jvrnt
  • 635
  • 1
  • 6
  • 20
-1

Alright if you really wanted to do this,

Route::get('renderEnvChaining',function(){
    $myCustomEnv = parse_ini_file(base_path(env('CUSTOM_ENV')));
   return $myCustomEnv['USE_SSL']; // This will return true
});

In my .env file i've mentioned this,

CUSTOM_ENV=.env.example

Now i in default env.example i put this

USE_SSL="true"  // This will return true
USE_SSL=true    // This will return 1

I just found that .ini and .env follows the same values, so i parsed .env file as .ini file,

Give this a try, i've never done this thing before, please concern with other developers before running this into production server

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
  • 2
    Several reasons. First, `.env` files are not `.ini` files. There are differences, like how you indicate a comment, so using `parse_ini_file` may cause issues. Second, your proposed approach doesn't populate environment variables. Third, it only functions on the single route in question. – ceejayoz Jul 19 '19 at 12:43
  • @ceejayoz Thank you for making me understood this clearly, Okay so what if i use this https://github.com/vlucas/phpdotenv package to load env file, would that be a good approach ? and i know they both are different and they work differently. Here i found good solution to load custom env with package https://stackoverflow.com/a/38824174/5928015 , last thing should i update my answer with using packages or should i remove it for misleading ? – Vipertecpro Jul 19 '19 at 14:57
  • Ultimately, I think the question itself is flawed as it's based off a misunderstanding of how `.env` is supposed to be used. Each computer (local dev or server) has its own `.env`. Yours might be different from mine, even for the same app. The solution to dev-specific settings is to just put them in the main `.env` file on the dev server. – ceejayoz Jul 19 '19 at 15:00
  • Yes that's what i get to learn from laravel doc. .env file is just for handle configuration, without .env we have to write values in each of the config folder files like database.php, etc, manually. So does anyone going to close this question ? because what framework gives as default we should always override and extends it's methods, right ? – Vipertecpro Jul 19 '19 at 15:07