Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Laravel 5 configuration - environments and overriding

I installed fresh Laravel 5 copy.

My detectEnvironment function is defined this way:

    return 'local';
    return getenv('APP_ENV') ?: 'production';

In config\local I've created database.php file:


return [
    'nothing' => 'new',

I run php artisan clear-compiled.

My index method of WelcomeController is defined this way:

public function index(Application $app)
    echo $app->environment();
    //echo $app['config']['database'];
    return view('welcome');

Application was imported this way: use Illuminate\Foundation\Application;

The result I get is:

local array(1) { ["nothing"]=> string(3) "new" } 

whereas I would expect Laravel to cascade config file with production one (with the default config\database.php file.

The strange thing is that even if I comment the line return 'local'; run again php artisan clear-compiled it shows:

production array(1) { ["nothing"]=> string(3) "new" } 

so it seems it always loads database.php file content (this one from local folder) and overrides main database.php file. It works fine again when I change this file name to for example aaa.php.

Is it a bug or maybe environment configuration shouldn't be stored inside config directory? But if not, where should they be store? I don't know if it's a bug or a feature so if anyone knows more about it, please give me a clue.

like image 250
Marcin Nabiałek Avatar asked Dec 07 '14 13:12

Marcin Nabiałek

People also ask

How do I override a configuration file in Laravel?

Laravel stores all the config file values into one single array. So, the "Laravel way" of overwriting the config variables after they are set is to edit the array in which they are stored: config([ // overwriting values set in config/app. php 'app.

How do I change environment variables in Laravel dynamically?

A simple way to update the . env key value in laravel is to add the below code in the controller function where you want to change . env values. $key = 'VARIABLE_NAME'; $value = 'NEW VALUE'; file_put_contents(app()->environmentFilePath(), str_replace($key .

What is App_env in Laravel?

You just need to set APP_ENV to whatever you want, for example: APP_ENV=development. This is used to identify the current enviroment. If you want to display errors, you'll need to enable debug mode in the same file: APP_DEBUG=true. The role of the .

What is config () in Laravel?

What is the config method? The config method allows your application to get or set values in all files that are in the config directory.

2 Answers

Although in documentation for Laravel dev (5.0) there is info that configuration will cascade it's not true. I have tested it about 2 weeks ago and it seems at the moment the only way to have different values for environments is using ENV file where you put custom values for current environment. Putting settings in directories won't work as it used to work however it's possible it will change or maybe has been already changed for last 2 weeks.

like image 192
Marcin Nabiałek Avatar answered Oct 02 '22 15:10

Marcin Nabiałek

There's a package that brings the cascading config system back to Laravel 5.

Disclaimer: I am the author.

like image 35
An Phan Avatar answered Oct 02 '22 15:10

An Phan