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 theIP
checking disabled when the checkbox was not checked on form submit. It means, if the user unchecks the checkbox then theIP
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 usedFile::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 usingConfig::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 thearray
to a parsable string representation of thearray
, just likeJSON
string and in this case thearray
gets wrapped intoquotes
, so following line gives me thearray
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 intoapp/config/customization.php
file. The first argument in theFile::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 thearray
including thePHP
tag andreturn
keyword and the;
which should be at the end of the thearray
. Which means, I've written somePHP
code into mycustomization.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.