如何存储Web应用程序的配置设置? [英] 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.
所以我在想,也许我可以在数据库中创建一个名为元"的表,然后在其中插入值(或元组)!然后,如果某个模块被关闭,脚本将更新该行,并将"module x"设置为0,这样我就完成了,对吧?
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.
粗略浏览一下drupal数据库不会产生任何结果,我猜想它们使用服务器本身上的配置文件吗?如果是这种情况,我不知道Web应用程序如何准确读取(例如).cfg文件中保存的值,也不知道这样的应用程序如何将信息保存到文件中.如果您之前已经解决了这个问题,我将不胜感激.
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.
顺便说一下,我主要使用PHP.
I use primarily PHP, by the way.
提前谢谢!
推荐答案
我经常看到使用配置数组完成此操作:
I've often seen this accomplished using a config array:
$config["admin_email"] = "admin@mydomain.com";
$config["site_name"] = "Bob's Trinket Store";
$config["excluded_modules"] = array("inventory", "user_chat");
然后您可以检查:
if (!in_array("inventory", $config["excluded_modules"])) {
// include inventory logic
}
当然,这有点倒退.实际上,显式声明 included 模块而不是否定声明会更明智.然后,您将在项目中引用此config.php
来加载并响应不同的配置.
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:
- 选项
- 值
其中option
可能是"excluded_modules",而其相应的value
将是"inventory,user_cat".不过,老实说,这种方法有点草率,将来可能会给您带来一些挫败感.
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.
这篇关于如何存储Web应用程序的配置设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!