Laravel Tips – Update and Persist Config on Runtime

As we all know that Laravel has provided an elegant way to setup application’s configuration using Config class which uses local file system. Using this component we are able to setup our application’s configuration and also we can get any value on runtime and can update it but it’s not persistent. It means, if we update any value in our configuration files which are stored in app/config folder then the change is only available for the current request, the value in the file system remains unchanged.

Sometimes we may need to update any configuration on runtime and we may need the change persistently. Well, a few days ago, I faced a similar need. Suddenly my client asked me to allow access to his web based accounting software only from a certain IP address. I did it by checking the client’s real IP address dedicated to his office but I needed a way to keep that settings persistently and I had no intention to change the database only for that so I’ve created a config file and keep the IP settings in that file. So, the way I did it as given below:

The code is very straightforward, I provided a inline form with a single text input field and a checkbox. The input field was for the IP address and the checkbox was for making the IP checking disabled when the checkbox was not checked on form submit. It means, if the user unchecks the checkbox then the IP checking should not be done and I could allow access to the application from anywhere. So, to update the settings in the file system, I’ve used File::put method to write the value. At first lets check the config file that I was using:

So when the form was submitting to my handler, I’ve just checked whether the IP address is given or not and then assigned the new value to the $array array which was retrieved using Config::get method to get the whole array from that file. The following code was used to assign new value:

Then on the next line I’ve updated the checkbox value using following code

After that I’ve used PHP‘s var_export function to convert the array to a parsable string representation of the array, just like JSON string and in this case the array gets wrapped into quotes, so following line gives me the array as string:

Finally I’ve just used this:

In this code, I wrote the content of $data variable into app/config/customization.php file. The first argument in the File::put method is the file name and the second argument is the data that I want to write and in this case the data is the string representation of the array including the PHP tag and return keyword and the ; which should be at the end of the the array. Which means, I’ve written some PHP code into my customization.php file and the code was written to the file is this:

That’s it. This way I’ve updated the config on runtime and kept it persistent. A little hacky but it works.

  • Nazim Mahmud Khan

    very nice and handy ..waiting to learn more and more from u..thanks a lot

  • Bruno Pires Lavigne Quintanilh

    In L5.2 would be better to edit the .env file, do you agree?

    • Probably not because config files contains arrays that is a generic data structure we can use easily with native functions but could be an option, not sure tho 🙂

      • Bruno Pires Lavigne Quintanilh

        Yes, i understand that, you are right. I was thinking about the lost documentation on default configuration files like database. A simple str_replace should be enough to edit the .env file. I will try it out.
        Thank you for your post.

        • You are most welcome, share your idea if you find that better 🙂

  • Juan Miguel Paulino

    Thanks for this. Now I have the doub, what about if the configuration file is cached?

    • Gurpreet

      Then call this Artisan class Artisan::call(‘config:cache’);

Latest Blog