1

I am trying to change the Laravel configuration variables based on environment, but I do not know how.

Code to get config is

Config::get( 'contants.api_url' );

I am trying to have a separate results for the live environment and the development environment.

Real life scenario

I need to override some config files, so everybody who is using the development environment, has the same base configuration. I cannot use the .env file for this, because it is not shared between the team and everybody has their own version of it.

What did I try:

I read here Laravel 5 configuration - environments and overriding that you can create subfolders within your /config folder, that match the environment, to override the default config files. Alas it did not work for me and the Laravel 5.4 documentation on the subject matter (https://laravel.com/docs/5.4/configuration#retrieving-environment-configuration) did not speak about this option, thus I believe it might have been deprecated.

Karl Johan Vallner
  • 3,980
  • 4
  • 35
  • 46

2 Answers2

1

One option could be to use a service provider. If you're only going to be overriding one or two values then you might as well just use your AppServiceProvider, otherwise I would suggest creating a new service (e.g. ConfigServiceProvider).

In the boot() method of your service provider you could add something like:

if (app()->environment() === 'local') {
   config()->set('contants.api_url', 'http://example.com');
}
Rwd
  • 34,180
  • 6
  • 64
  • 78
  • I upvoted, but I do not particularly like the design of this approach. Are you sure Laravel has no core functionality akin to this? https://github.com/machaven/laravel-cascading-config. If not, then I will mark it as an answer.. – Karl Johan Vallner Dec 23 '18 at 12:31
  • @FifthEon No, I don't believe Laravel has this functionality anymore but I could be mistaken. To be fair, the github repo you mentioned above is a very similar approach to what I've suggested. With the example you gave in your comment I assumed you were only going to be changing one or two values so I didn't really see the point in showing a more elaborate system but if you look at the repo it's only really providing a service provider https://github.com/machaven/laravel-cascading-config/blob/master/src/CascadingConfigProvider.php which again checks the env and replaces the config values. – Rwd Dec 23 '18 at 13:47
  • thats true, I just don't like the idea of incorporating non-scalable solutions into our big application. But again, it's probably only going to be a few variables yes, so I will use that. – Karl Johan Vallner Dec 23 '18 at 15:42
1

To change the configuration variables based on environment, this will do:

Example file: config/database.php

       'mysql' => [
            'driver' => mysql,
            'url' => env('DATABASE_URL'),
            'host'=> (env('APP_ENV') === 'local' ? env('DB_HOST_LOCAL') : env('DB_HOST_PRODUCTION')),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'default'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'options' => (env('APP_ENV') === 'local' ? [
                'some_option' =>  env('SOME_ENV')
            ] : []),
        ],

Tested and works on Laravel 5.8.

André Gollubits
  • 163
  • 1
  • 3
  • 13