Tutorial

How to Use Laravel Config Files

Draft updated on Invalid Date
author

Samuel Oloruntoba

How to Use Laravel Config Files

This tutorial is out of date and no longer maintained.

Introduction

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 to use a 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.

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.

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 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.

The Config Facade

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 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.

Setting a Config Value

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.

The config Function

The 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'
]);

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 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.

Conclusion

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.

Learn more about our products

About the authors
Default avatar
Samuel Oloruntoba

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.