8
Php

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:

public function updateIpSettings()
{
    $array = Config::get('customization');
    if(Input::has('ip_address')) {
        $array['ip_settings']['ip_address'] = Input::get('ip_address');
    }
    $array['ip_settings']['ip_check'] = Input::has('ip_check') ? 1 : 0;
    $data = var_export($array, 1);
    if(File::put(app_path() . '/config/customization.php', "

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:

return array (
    'ip_settings' => array ( 'ip_address' => '127.0.0.1', 'ip_check' => 1 )
);

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:

$array = Config::get('customization');
if(Input::has('ip_address')) {
    $array['ip_settings']['ip_address'] = Input::get('ip_address');
}

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

$array['ip_settings']['ip_check'] = Input::has('ip_check') ? 1 : 0;

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:

$data = var_export($array, 1);

Finally I've just used this:

if(File::put(app_path() . '/config/customization.php', "

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:

"return array('ip_settings' => array ( 'ip_address' => '127.0.0.1', 'ip_check' => 1 ));"

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

Latest Blog

0
Php

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 […]