Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store configuration settings for web app?

I have some site metadata I'd like to be changeable... for example, in my application, if the sysadmin didn't want to use the "Inventory" portion of the site, he/she could turn it off, and it would disappear from the main site.

So I was thinking, maybe I could make a table in my database called "meta", and insert values (or tuples) there! Then, if a module got turned off, the script would update the row, and set "module x" to 0, and I'd be done with it, right?

Except it seems like an awful lot of overhead (creating an entire table, and maintaining it, etc) just for a set of values... Basically, my solution sounds like shoving a square peg into a circular slot.

A cursory glance over the drupal database yielded nothing, and I'm guessing they use a configuration file on the server itself? If that's the case, I don't know exactly how saved values in a .cfg file (for example) could be read by the web app, nor do I know how such an app could save information to the file. I'd appreciate your insight, if you've tackled this problem before.

I use primarily PHP, by the way.

Thanks in advance!

like image 369
Julian H. Lam Avatar asked Sep 06 '25 03:09

Julian H. Lam


1 Answers

I've often seen this accomplished using a config array:

$config["admin_email"] = "[email protected]";
$config["site_name"] = "Bob's Trinket Store";
$config["excluded_modules"] = array("inventory", "user_chat");

Then later you can check:

if (!in_array("inventory", $config["excluded_modules"])) {
  // include inventory logic
}

Granted, this is a bit backwards. In reality, it would be smarter to explicitly declare included modules, rather than the negative. You would then reference this config.php in your project to load-up and act in response to different configurations.

You could implement this as a database table too, making at least two fields:

  1. Option
  2. Value

Where option may be "excluded_modules" and its corresponding value would be "inventory,user_cat". In all honesty though, this method is a bit sloppy, and may cause you some frustration in the future.

like image 63
Sampson Avatar answered Sep 07 '25 20:09

Sampson