How To Create Custom Configuration File In Laravel

While developing web application configuration plays major role. Laravel framework makes easy to handle configurations for complex application with config files. All configuration files for Laravel framework is stored in config directory.

In Laravel, you can create custom config files as per your requirement. You can also use config() helper to access configuration values for any config file.

The main reason to use config files is it's stores as global values and changes in those values can easily handles with config files otherwise we need to change those values in actual files where we statically used. Laravel will also cache those values so it will impact on performance positively.

In this example we will create new custom file for google login.

Creating Config File

First of all, let's create new pgp file at config directory and name it as google.php. In this file, we will just store one value for this example.

Here, we just have created file which returns array containing API token as key. For value of that key we used Environment variable. We have also configured default value if environment variable is not found.

Let's add environment variable into our env file. Open env file from root directory and add below line:

Using Custom Config File In Laravel

We will use config helper to get value form our custom config file.

Here, we can also get nested values using . access modifier.

Conclusion

Here, We have just have created simple config file which contains only one value and retrieve it using config helper in Laravel. In actual application, there can be more then few values or nested value as per application's requirement.


Share your thoughts

Ask anything about this examples