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.

Latest Blog


PHP – 8.0 Match Expression

In PHP 8.0 there is a new feature or I should say a new language construct (keyword) going to be introduced,  which has been implemented depending […]


New Union Type in PHP – 8

A new RFC has been finished voting and accepted for PHP – 8. That is “Union Types”, which is an extension of PHP’s type system. […]