How to use Config file in Laravel

0
1314

All of the configuration files for the Laravel framework are stored in the config directory. There are a lot of configuration files by default such as database, mail, queue and so on. We can create our own config file that can be used to set options in our application.

Create Config file

To create a configuration file, we just need to create a file inside config folder and return an array with key-value pair.

 true;
    // OR
    'demo' = env('DEMO', true)
];

We can also use multi-dimensional array to set the value.

 [
        'url' => 'https://www.facebook.com/kodementor',
        'username' => 'kodementor'
    ],
    'twitter' => [
        'url' => 'https://twitter.com/kodementor',
        'username' => 'kodementor'
    ]
];

Note that the second example pick up the value from .env file. If this is not set, it will take default value true. So, we just need to set option as below in .env file.

// .env
....
DEMO = false
....

Get Config value

We can get the cofiguration values using the Config facade. For example:


echo Config::get('mode.test');
// OR
echo Config::get('social.facebook.url');

There is also another method to get config value. We can use config function. For example:

echo config('mode.test');
// OR
echo cofig('social.facebook.url');

Be sure to clear the cached config because config files are cached for faster performance. To know more, here is a nice article explaining all the methods about clearing cache in laravel.

That’s it. Isn’t it pretty easy to set and retrieve cofiguration file in laravel? Drop your thoughts below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.