You discover that storing files on your own server is extra load, so not ideal. You open your web browser and do a google search on "efficient ways to store files". Finally, you decide to use Amazon S3. Since you are a Laravel user, you open
config/filesystems.php, and then you change the default key value from
s3, fill in your API keys. Just like that, you have your files uploaded to Amazon S3.
Magic, no. This is an example of a configurable system, and an example of SOLID programming. By setting some array values in a file, we can get a system to work without breaking anything. This article is not about SOLID, we are here to talk about Laravel configuration files and how to use them.
Configuration files in Laravel provide an easy way to set options required by parts of our application to work. Laravel has a lot of configuration files in the
config directory. We as developers also have the option to create our own configuration file(s).
Table of Contents
When to use Config file
When building a Laravel Package, config files go a long way in helping you build a configurable and functional package.
Creating a Configuration File
One reason developers love Laravel is the fact that it makes everything simple. Creating configuration files is not an exception to that rule. To create a configuration file, go to the
config directory and create a file there. For our example, let's call our file
social.php. After creating the file, open it and return an empty array. There, we have created a configuration file.
Now that we have our config file, we need to set options. To do that, we set array key values as our options.
return [ 'facebook' => 'https://www.facebook.com/scotchdevelopment', 'twitter' => 'https://twitter.com/scotch_io' ];
We can even use multi-dimensional arrays too.
return [ 'facebook' => [ 'url' => 'https://www.facebook.com/scotchdevelopment', 'username' => 'scotchdevelopment' ], 'twitter' => [ 'url' => 'https://twitter.com/scotch_io', 'username' => 'scotch_io' ] ];
Using Config Variables
There two ways with which we can access config variables present in our config file. The first method we will talk about is using Laravel's inbuilt
To get the option 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 facebook's page URL, we would use something like this.
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.
Passing the second parameter to the
get method means that you want to return a default value if the config value does not exist.
Setting a Config Value
If during the application lifecycle, you want to change a config file value. You can use the facade
We could also pass an array and use that to set values.
Config::set([ 'social.facebook.url' => 'http://example.com' ]);
NOTE: Trying to set an inexistent config value would not work.
config function is exactly like the
Config facade. To get an option, we do.
To set an option, we do.
config([ 'social.facebook.url' => 'http://example.com' ]);
Using Environment Variables with Config Files
There are times when we need sensitive data in our config files. Data that we don't want to show up in our git repo. These data include API keys, usernames, and passwords. To get around this problem, we can use environment variables. Don't know what an environment variable is, check this article. Opening our config file, we can add an API key section to our
[ ... 'facebook' => [ 'url' => 'https://www.facebook.com/scotchdevelopment', 'username' => 'scotchdevelopment', 'api_key' => '138764782163845817345162837412' ], ... ]
When you push your site to git or whatever version control you use, this gets stored forever. A more suitable solution would be to use environment variables. Thankfully, in Laravel we can create an environment variable in our
.env file and reference it in our code whenever we need it. So we create a new
FACEBOOK_API_KEY in our .env file and use it in our application like this.
[ .... 'facebook' => [ 'url' => 'https://www.facebook.com/scotchdevelopment', 'username' => 'scotchdevelopment', 'api_key' => env('FACEBOOK_API_KEY') ], .... ]
This is just so much better.
Config files are helpful when developing in Laravel, especially when building Laravel Packages. Since Laravel provides an easy to create and use them, it makes them even more good to use.