Using Config files in Laravel 5.4

Laravel provides an easy way to manage the application configration through its config files.In Laravel, all configurations files are stored or kept in config directory of your application.

#Structure of Config Directory

After installing fresh laravel the config directory looks like below

config directory structure in Laravel
config directory structure in Laravel

Why to use config files in laravel

Config files makes laravel application more configrable and modular.

#Creating a Configuration File in Laravel

To create a configuration file in Laravel, go to the config directory of your application and create an empty .php file there.For example let’s create an config file for site config.

#Steps to create a config file in laravel 5.4

In this example we are going to create site configration config file that is use to keep information about website or app.Lets start

  • Step 1:Create an empty site.php file under config directory.
  • Step 2: Adding options to configration file .You can add option to your coonfigration file in key value format.
return [
    'site_name'    => 'Laravel interview questions',
    'site_tagline' => 'A complete interview guide and tutoorial foor laravel'
    'site_owner'   => 'Laravelscots'
];

There are two ways to get OR set config variables in laravel.

  1. Using Config Facade
  2. Using Config Function

Setting or getting Config data in Laravel using Config Facade

#Getting config Variables in Laravel 5.4 using Config Facade

You can get your config variable by using get method on config Facade
Usage:

echo Config::get('site.site_name')

# Setting config Variables in Laravelo 5.4 using Config Facade

You can get your config variable by using get method on config Facade
Usage:

echo Config::set('site.site_name', 'laravel interview question');

Setting or getting Config data in Laravel using Config function

The config function is exactly like the Config facade.

#Getting config Variables in Laravel 5.4 using Config Function

To get an option using config function we simply use below syntax
Usage:

echo config('site.site_name');

# Setting config Variables in Laravel 5.4 using Config Function

Usage:

echo Config('site.site_name', 'laravel interview question');

That’s all, Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *