DB Configuration Registry

article_normal_settings.jpg

In any application at some stage you would want to add dynamic configuration options per installation. This should be easy to change from a front-end ui and easy to collect from the development side. PHPDevShell offers a very quick and fast way to collect configuration data for a specific plugin. It allows you to enter data and retrieve it from the development side.

With this tutorial we will assume that you have a working plugin, if you don't have a look at the ExamplePlugin for a working version of this tutorial.

01Lets add a few things to the configuration repository, to do this, simple open PHPDevShell UI and browse to System Management->Config Manager, now we will add a setting or two.

When adding your setting for your plugin, always add the plugin name as the prefix, something like this ExamplePlugin_, this tells PHPDevShell where exactly to find the settings and also eliminates same names used. So for instance, so lets add the following settings;


Setting Name : ExamplePlugin_sampleSetting1
Setting Value : Just some random data.
and
Setting Name : ExamplePlugin_sampleSomethingElse
Setting Value : Another setting example

02Now that you see both setting in the settings repository, go to your controller script and call them. Note that you will be using the ExamplePlugin that you used in the prefix to call your plugin. If the second value is left empty the system will assume the current running plugin as the prefix.

$setting = $this->db->getSettings(array('sampleSetting1', 'sampleSomethingElse'), 'ExamplePlugin');

You have now requested the system to call the two values from the settings registry and store them into an array, to use them now is quite easy;

echo $setting['sampleSetting1']; // Will echo "Just some random data."
echo $setting['sampleSomethingElse']; // Will echo "Another setting example"

You have now learned the basics of using the configuration registry.

Improving ease of use

At a later stage, you might want to build a UI specific for a plugins settings, especially if noob admin staff will change settings. This is quite easy to do as there are two more methods you will have available to easily store and update data. These are;

$this->db->writeSettings(array('sampleSetting1'=>'value', 'sampleSetting2'=>'value2'),'ExamplePlugin');
$this->db->deleteSettings(array('sampleSetting1', 'sampleSetting2'), 'ExamplePlugin');

This concludes the basic usage of the configuration registry, now it is up to you and your imagination on how you could use this in a productive environment to make your plugin more sustainable.

PHPDevShell © 2010 - All rights reserved.