Best way to store a PHP App's Settings?

前端 未结 10 1816
南笙
南笙 2020-12-08 11:26

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

相关标签:
10条回答
  • 2020-12-08 12:20

    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.

    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?

    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.

    0 讨论(0)
  • 2020-12-08 12:21

    We just use

    $siteConfig['db_name'] = 'database1';
    $siteConfig['site_name'] = 'Stackoverflow';
    

    In a included php file. Putting the values in a array helps with name conflicts.

    0 讨论(0)
  • 2020-12-08 12:21

    What has mentioned before, is true. I like the one more than the other, but I mostly use another way of storing my configuration.

    I never use a database as the place to store my settings, because that would create a lot of data transfers, which can make the application a little more insecure- in my opinion. Besides, some application hosts (like Amazon's AWS and Google's Cloud Platform) limit the read/write actions to a database.

    Therefore, I mostly use this method:

    Firstly, I create a file config/settings.php with the following contents:

    <?php
    return [
        'database' => [
            'host' => 'localhost',
            'port' => 3006,
            'user' => 'username',
            'password' => // your secret password
        ],
        'application' => [
            'name' => 'Your site\'s name',
            'version' => '1.0-dev'
        ]
    ]
    

    When you want to use this in you index.php file, add the following line in it:

    $config = include('./config/settings.php');
    

    I hope this can add some information for you or others.

    0 讨论(0)
  • 2020-12-08 12:24

    Just got done chatting with a few people on IRC about this. I looked at how Wordpress handled this after I pulled up a SQL dump of one copy. I think I'll use this layout and rename the columns a bit. But the idea is...

    option_id | option_name | option_value | autoload
    int       | varchar     | longtext     | varchar
    (PRIMARY) | (UNIQUE)    |              |
    
    0 讨论(0)
提交回复
热议问题