How to Use Laravel Config Files

How to Use Laravel Config Files

Good day, I guess you are having a "codie" day.

Today i started the recreation of an app assigned to me by my boss. This app was created with laravel 5. I have been instructed to create a new version of the app with Laravel 8. Upon starting of the application, I realized the application would't even start without the presence of "settings" table in the database. Which basically contains all the settings of the application such as company name, Site name, Site title, company email etc.

This kind of situations are what warrants adding a config file for your application in order to prevent that kind of situation. The implementation of this style is what we refer to as SOLID configuration. Laravel has made this easy to implement, laravel has a lot of configuration file in the Config directory. As a developer you can create config files of your own. Now let get down to business.

Creating a config file

One of the many reasons I love laravel is the fact that it makes a lot of things simple. Creation of configuration files is not an exception to that rule. To create a configuration file, go to the config directory and create a file there or run the below code in your app folder

touch Config/appsettings.php

For our example, let's call our file appsettings.php. After creating the file, open it and return an empty array like the image below. Below, we have created a configuration file.

<?php
 return [

 ];
?>

Adding options

Now that we have our config file, we need to set options. To do that, we set array key values as our options.

<?php
 return [
     "site_name" => "Teeque",
     "company_name" => "Teeque Technologies",
     "contact_email" => "realolamilekan@gmail.com",
     "contact_number" => "07086556010",   
 ];
?>

We can also set multidimensional arrays like this

<?php
 return [
         "twitter" => [
        "url" => 'https://twitter.com/ibreathcode',
        "username" => 'ibreathcode'
         ],
        "hashnode" => [
        "url" => "https://teeque.hashnode.dev",
        "username" => "@teeque"
         ]
 ];

Using Config Variables

There are two method with which we can access config variables present in our config file. The first method is using Laravel's inbuilt Config facade and using config function.

The Config facade

In order to get any of the options in our config file, we call a static get method on the Config facade. To get the value, we use dot notation. To get the hashnode page URL, we would use something like this.

echo Config::get('hashnode.url')

But, this won't work just yet. We need to tell the facade what configuration file to use. To do that, just enter the name of the configuration file without .php and a dot after like this.

echo Config::get("appsettings.hashnode.url");

You can also pass the second parameter to the get method in other to set a default value if the config value does not exist.

echo Config::get("appsettings.hashnode.url", "hashnode.com");

The Config function

The config function is exactly like the Config facade. To get an option, we do.

echo config('appsettings.twitter.url');

Wrapping up

You can also set config files using the config facade

Config::set('appsettings.twitter.username', 'johndoe');

Or using the config function

config('appsettings.twitter.username', 'johndoe');