如何存储网络应用程序的配置设置?

发布于 2024-08-19 17:49:27 字数 434 浏览 7 评论 0原文

我有一些站点元数据我希望可以更改...例如,在我的应用程序中,如果系统管理员不想使用站点的“库存”部分,他/她可以将其关闭,并且它将从主站点消失。

所以我在想,也许我可以在数据库中创建一个名为“元”的表,并在那里插入值(或元组)!然后,如果模块被关闭,脚本将更新该行,并将“模块 x”设置为 0,这样我就完成了,对吗?

除了对于一组值来说似乎需要大量的开销(创建整个表并维护它等)......基本上,我的解决方案听起来像是将一个方形钉子推入一个圆形槽中。

粗略地浏览了一下 drupal 数据库,没有发现任何结果,我猜他们在服务器本身上使用了配置文件?如果是这种情况,我不知道 Web 应用程序如何读取 .cfg 文件(例如)中保存的值,也不知道此类应用程序如何将信息保存到文件中。如果您以前解决过这个问题,我会很感激您的见解。

顺便说一句,我主要使用 PHP。

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还在原地等你 2024-08-26 17:49:28

基本上您有两种选择 - 要么将其放入数据库表中,要么放入平面配置文件中(可能是 PHP,也可能是 XML)。对于后者,要使其可从页面进行编辑,您必须 (1) 处理混乱的操作系统特定文件访问问题,(1) 每次设置站点时应用适当的文件权限,以及 (3) 解析和生成 PHP/XML 代码。对于数据库,您所需要的只是一个简单的查询,所以我肯定会这样做。

至于使用这种方法的大型项目,我知道 phpBB 确实将其大部分配置存储在数据库中(除了密码,我上次检查时)。

You have two choices basically - either put it in a DB table, or in a flat config file (probably PHP, perhaps XML). With the latter, to make it editable from a page, you will have to (1) deal with messy OS-specific file access issues, (1) apply proper file permissions each time you set up a site, and (3) parse and generate PHP/XML code. With a database, all you need is a simple query, so I'd definitely go with that.

As for large projects using this approach, I know phpBB does store most of its config in a database (except for passwords, last time I checked).

爱殇璃 2024-08-26 17:49:28

我更喜欢使用 ini 文件作为位于 public_html 文件夹之前的配置。我认为这给了我很大的灵活性和分组 var,并在必要时为模块等创建单独的 ini。

I prefer to work with ini files as configuration that sit before public_html folder.I think that gives me a lot of flexibility and grouping var and create if necessary separate ini for modules etc.

遮了一弯 2024-08-26 17:49:27

我经常看到使用配置数组完成此操作:

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

然后稍后您可以检查:

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

当然,这有点倒退。实际上,显式声明包含模块会更明智,而不是否定。然后,您可以在项目中引用此 config.php 来加载并响应不同的配置。

您也可以将其实现为数据库表,至少创建两个字段:

  1. Option
  2. Value

其中 option 可能是“excluded_modules”,其相应的 value 将为“inventory,user_cat” 。但老实说,这种方法有点草率,并且可能会在将来给你带来一些挫败感。

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.

断桥再见 2024-08-26 17:49:27

我知道您的问题是“如何从网络应用程序读取/写入服务器上的单独文件”,但我想我应该解决您所做的假设之一。将配置存储在数据库中并没有什么问题。

我见过将配置存储在数据库中的项目(具有大量流量和良好的正常运行时间 - 以及大量 IT 保持这种方式=P),或多或少如您所描述的那样。如果它是一个表,并且你没有一个完整的疯狂的故障转移/分区方案,那么它实际上并没有那么大的开销。

除了存储数据之外,数据库还有很多功能以及围绕它的许多基础设施。如果您使用数据库进行配置,则可以使用任何数据库部署/备份机制,而无需额外成本。您还可以利用内置的权限机制以及任何可用的撤消功能。

编辑:
但是,如果您在每个页面显示上访问该配置,那么您可能会遇到瓶颈:) 关于您的设计。一种解决方案是,如果您有持久性 Web 服务,则可以让它每 X 秒重新扫描一次配置。

I know your question is "how do I read/write to a separate file on the server from a web app", but I figured I'd address one of the assumptions you made. There's nothing (too) wrong with storing your config in the DB.

I've seen projects (with lots of traffic, and good uptime - and a ton of IT keeping it that way =P) that stored configuration in the database, more or less as you described. If it's a single table, and you don't have a whole crazy fail-over/partitioning scheme on it, then it's not really THAT much overhead.

The DB has lots of features, besides storing data, and a lot of infrastructure around it. If you use the DB for your config, you get to use whatever mechanism you have for DB deployment/backup with little extra cost. You also can take advantage of the built in permissions mechanism, and any undo features that are available.

Edit:
However, if you access that config on every page display, though, you might bottleneck :) All about your design. One solution is that if you have a persistent web service, you can have it re-scan the config every X seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文