【问题标题】:What is the best way to edit a settings file containing a global named config in php在php中编辑包含全局命名配置的设置文件的最佳方法是什么
【发布时间】:2017-05-15 23:06:23
【问题描述】:

我有文件'init.php'。这个用于启动会话、设置一些设置和诸如此类的东西。我使用此行调用此文件:

require_once 'core/init.php';

在我看来,这非常有效。现在,我编写了以下脚本,以便在 init.php 文件中调用设置变得非常容易。

class Config {
    public static function get($path = null) {
        if ($path){
            $config = $GLOBALS['config'];
            $path = explode('/', $path);

            foreach($path as $bit) {
                if(isset($config[$bit])) {
                    $config = $config[$bit];
                }
            }    
            return $config;
        }    
        return false;
    }
}

所以现在我可以在我的其他页面中使用这行代码来使用设置:

Config::get('settings/main_color')

这当然很容易。但现在我想编辑一个设置,而不必自己更改文件。这一切都应该由浏览器中的脚本完成。我的 init.php 设置全局的其余部分如下所示:

$GLOBALS['config'] = array(
    'mysql' => array(
        'host' => 'localhost:3307',
        'username' => 'root',
        'password' => 'usbw',
        'db' => 'webshop'
    ),
    'remember' => array(
        'cookie_name' => 'hash',
        'cookie_expiry' => 604800
    ),
    'sessions' => array(
        'session_name' => 'user',
        'token_name' => 'token'
    ),
    'settings' => array(
        'main_color' => '#069CDE',
        'front_page_cat' => 'Best Verkocht,Populaire Producten',
        'title_block_first' => 'GRATIS verzending van €50,-',
        'title_block_second' => 'Vandaag besteld morgen in huis!',
    ),
    'statics' => array(
        'header' => 'enabled',
        'title_block' => 'enabled',
        'menu' => 'enabled',
        'slideshow' => 'enabled',
        'left_box' => 'enabled',
        'email_block' => 'enabled',
        'footer' => 'enabled',
        'keurmerken' => 'enabled',
        'copyright' => 'enabled'
    )
);

我希望是这样的解决方案:

Config::update('settings/main_color','blue')

实现这一目标的最佳方法是什么?使用 str_replace 并替换文件中的单词?我希望有更好的方法来做到这一点,如果你能帮助我,我会非常高兴。

编辑:(我完整的 init.php 文件)

<?php
session_start();

define('DS',DIRECTORY_SEPARATOR);

$GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);

spl_autoload_register(function($class) {
    require_once 'classes/' . $class . '.php';
});

require_once 'functions/sanitize.php';
require_once 'functions/statics.php';
require_once 'functions/pagination.php';

if(Cookie::exists(Config::get('remember/cookie_name')) && !Session::exists(Config::get('sessions/session_name'))) {
    $hash = Cookie::get(Config::get('remember/cookie_name'));
    $hashCheck = DB::getInstance()->get('users_session', array('hash', '=', $hash));

    if($hashCheck->count()) {
        $user = new User($hashCheck->first()->user_id);
        $user->login();
    }
}

$_link = DB::getConnected();

$url_parts = explode('/',$_SERVER['REQUEST_URI']);
$current = $url_parts[count($url_parts)-2];

if($current == 'page'){
    $_SESSION['location'] = 1;
}
else{
    $_SESSION['location'] = 0;
}

【问题讨论】:

    标签: php settings global


    【解决方案1】:

    这有点基于意见,但我使用配置文件解析的 json 或 xml 文件。 update 会调用该文件,对其进行解析然后重新保存。

    /core/init.php

    $GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);
    

    /core/prefs.json

    {"mysql":{"host":"localhost:3307","username":"root","password":"usbw","db":"webshop"},"remember":{" cookie_name":"hash","cookie_expiry":604800},"sessions":{"session_name":"user","token_name":"token"},"settings":{"main_color":"#069CDE", "front_page_cat":"Best Verkocht,Populaire Producten","title_block_first":"GRATIS verzending van €50,-","title_block_second":"Vandaag besteld morgen in huis!"},"statics":{"header":"已启用","title_block":"已启用","菜单":"已启用","幻灯片":"已启用","left_box":"已启用","email_block":"已启用","页脚":"已启用" ,"keurmerken":"已启用","版权":"已启用"}}

    更新的伪代码:

    class Config
        {
            public static function update($path,$value)
                {
                    # Fetch the file and convert it to json
                    $file = json_decode(file_get_contents(CONFIG_PATH),true);
                    # Do some code that will update the value....
    
                    # Save file back
                    file_put_contents(CONFIG_PATH,json_encode($data));
    
                    # Call your prefs to GLOBALS function here so the settings
                    # are updated in your global array
                }
        }
    

    【讨论】:

    • 你知道不使用 JSON 的另一种方法吗?
    • XML 文件,但 Json 更直接
    • 但是当我使用 get 函数时,这种方法 (JSON) 是否仍然有效?
    • 是的,因为您将文件存储为 json 并将其解析为 ini 文件中的数组。
    • 另一种选择是将其中一些数据存储在数据库中,但连接凭据存储在您的数据库中没有意义。如果你想要一个静态首选项文件/配置,我会做 json,这样你就可以通过界面轻松编辑它
    猜你喜欢
    • 2012-09-29
    • 2023-03-25
    • 2015-04-30
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多