This tutorial is out of date and no longer maintained.
You discover that storing files on your own server is an 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 local
to 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).
When building a Laravel Package, config files go a long way in helping you build a configurable and functional package.
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.
return [];
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'
]
];
There are 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 built-in Config
facade.
Config
FacadeTo 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 page’s URL, we would use something like this.
echo Config::get('facebook.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('social.facebook.url');
Passing the second parameter to the get
method means that you want to return a default value if the config value does not exist.
If during the application lifecycle, you want to change a config file value. You can use the facade set
method.
Config::set('social.facebook.url', 'url');
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
FunctionThe config
function is exactly like the Config
facade. To get an option, we do.
echo config('social.facebook.url');
To set an option, we do.
config('social.facebook.url', 'http://example.com');
or
config([
'social.facebook.url' => 'http://example.com'
]);
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
array.
[
...
'facebook' => [
'url' => 'https://www.facebook.com/scotchdevelopment',
'username' => 'scotchdevelopment',
'api_key' => '123456789012345678901234567890'
],
...
]
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 better to use.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!