Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Config variable in Laravel 5.1

In my config/app.php file, I have added some variable and I have used these variable in my controller and view files. See below variables:

'META_TITLE' => 'title'
'META_KEYWORDS' => 'keyword'
'META_DESCRIPTION' => 'description'

and I have used these variables like this Config::get("app.META_TITLE")

But I want to override those variable in any of my controller as per requirement.

like image 224
okconfused Avatar asked Dec 11 '15 13:12

okconfused


2 Answers

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.META_TITLE'       => 'new meta title',
    'app.META_KEYWORDS'    => 'new meta keywords',
    'app.META_DESCRIPTION' => 'new meta description',

    // in case you would like to overwrite values inside config/services.php
    'services.facebook.client_id'     => 'client id',
    'services.facebook.client_secret' => 'client secret',

]); 

With this concept you can edit any variable set in any config file - just specify which config file they are stored in.

like image 89
Chris Avatar answered Sep 19 '22 12:09

Chris


For Laravel 5.1, from the docs (also works in 5.8)

To set configuration values at runtime, pass an array to the config helper:

config(['app.timezone' => 'America/Chicago']);

But note that if you want to override environment variables in Dusk, this approach helps: https://laracasts.com/discuss/channels/testing/how-to-change-env-variable-config-in-dusk-test?page=1#reply=475548

like image 21
Ryan Avatar answered Sep 18 '22 12:09

Ryan