存储PHP应用程序设置的最佳方法?

存储PHP应用程序设置的最佳方法?

问题描述:

为自定义PHP应用程序存储一组全局设置的最佳方法是什么?我正在做一个个人项目(实际上是第一个主要项目),并且需要一种存储键值对的方法来记录应用程序的总体设置.

What is the best approach to storing a group of global settings for a custom PHP application? I am working on a personal project (first major one really), and need a method of storing key-value pairs for recording overall settings for the application.

要存储为...的东西

  • 网站的全局名称.
  • 主题(只是变量或主题的路径)

我应该把它们放在一张桌子里吗?如果是这样,从boostrap查询它们的最佳方法是什么?除了对每个所需设置进行一次查询.

Should I just keep them in one table? If so what is the best way to query them from a boostrap? Besides doing a single query for each desired setting.

更新: 是的.ini或解析包含文件会很好,而且我知道该怎么做.但是我想知道将它们与其他所有内容一起存储在MySQL中的最佳方法是什么.

UPDATE: Yes a .ini or parsing an include file would be nice, and I know how to do it that way. But I wanted to know what would be the best approach to storing them in MySQL with everything else.

UPDATE2: 我之所以问这个也是因为我计划通过管理员界面更改许多这些设置.因此,如果您要更改网站的标题,则会立即进行更新,我认为最好通过SQL进行,因此需要在数据库内部进行设置.

UPDATE2: The reason I ask this also is I plan for a lot of these settings to be changeable through the Administrator interface. So if you were to change the Title of the site, it would be updated right away, which I figured would be best to do through SQL, thus needing setting inside the DB.

您是否考虑过将它们放入.php文件并将其包含在需要使用它们的页面上?为变量指定一个唯一的名称,以避免命名冲突.

Have you thought about putting them in a .php file and including it on the pages you need to use them? Give the variables a unique name so avoid naming conflicts.

由于您将在PHP应用程序中重复使用它们,因此这是最理想的选择.这也避免了将数据库存储在数据库中时进行数据库调用的需要.

Since you'll be using them repeatedly in your PHP application, this would be most ideal. This also avoids the need to make database calls if you were to store them in a database.

AppSettings.php

<?php
$_SITENAME_ = 'MyWebsite';
$_THEME_ = 'Theme/Path';
?>

更新:

我假设您希望这些设置可通过网页进行编辑,并且不希望有多个数据库查询,因为这些设置会更改,但不会经常更改?

UPDATE:

I assume you want these settings to be editable via a web page and don't want multiple DB Queries since these settings will change, but not too often?

我个人采取的一种方法是序列化AppSettings表并将其存储在XML文件中.当然,每次更新表时,都会对表进行重新序列化并将其存储在XML文件中.然后,我创建了一个单独的类来解析XML文件并返回所需的特定值.

One approach I personally took was to serialize the AppSettings table and store it in a XML file. Of course, every time the table is updated, the table would be reserialized and stored in the XML file. Then I created a separate class that parses the XML file and returns the specific values I needed.