How to store site wide settings in a database?

前端 未结 9 2858
时光说笑
时光说笑 2021-02-19 10:08

I\'m debating three different approaches to to storing sitewide settings for a web application.

A key/value pair lookup table, each key represents a setting.

    <
9条回答
  •  灰色年华
    2021-02-19 10:29

    A mixed approach is best. You have to consider what is best for each setting - which largely boils down to who would change each site-wide setting.

    If you have a development server and a live server, adding new application settings can be awkward if they are solely in the db. You either need to update the database before you update the code, or have all your code handle the situation where a setting is unavailable. Obviously one common sitewide setting is the database name, and that can't be stored in the database!

    You can easily end up with different settings in your test and live environments. I've taken settings away from the DB and into text files before now.

    I would recommend having defaults in a 'hardcoded' file, which may then overridden by a key/value pair lookup table.

    You can therefore push up new code without first needing to change the settings stored in the database.

    Where there are a varying amount of values, or values that are always changed at the same time, I'd store the values as JSON or other serialised form.

提交回复
热议问题