2021-12-07 by Lonnie Ezell

Filed under:

About a month ago, we released the initial version of the CodeIgniter Settings package, that allows you to manage configuration settings in your database with a fallback to the config files. It is a simple solution to a problem we all face.

Thanks to the hard work of MGatner, one of the core developers and an active package developer, I'm happy to announce a version 2 already.

Contextual Settings

The first version of the library was great for what it did, but it pretty much had a single purpose - your app configuration settings. Version 2 introduces Contextual Settings, that allow settings to be saved and grouped together. When given a context they do not need to fall back to a config file. This allows things like save settings on a user-by-user case for example.

An example... Say your App config includes the name of a theme to use to enhance your display. By default, your config file specifies App.theme = 'default'. When a user changes their theme, you do not want this to change the theme for all visitors to the site, so you need to provide the user as the context for the change:

$context = 'user:' . user_id();
service('setting')->set('App.theme', 'dark', $context);

Now when your filter is determining which theme to apply it can check for the current user as the context:

$context = 'user:' . user_id();
$theme = service('setting')->get('App.theme', $context);

// or using the helper
setting()->get('App.theme', $context);

Contexts are a cascading check, so if a context does not match a value it will fall back on general, i.e. service('setting')->get('App.theme'). Return value priority is as follows: "Setting with a context > Setting without context > Config value > null".

Drop a note on the forums to let us know what other uses you find for it.