dongyu7074 2017-01-01 15:51
浏览 111
已采纳

在php中编辑包含全局命名配置的设置文件的最佳方法是什么

I've got the file 'init.php'. This one is being used to start the session, set some settings and that sort of stuff. I call to this file using this line:

require_once 'core/init.php';

This works perfectly in my opinion. Now, I've written the following script so that it becomes very easy to call a setting in the init.php file.

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;
    }
}

So now I can use this line of code in my other pages to use the setting:

Config::get('settings/main_color')

This is very easy of course. But now I'd like to edit a setting without having to change the file myself. It should all be done by scripts in the browser. The rest of my init.php settings global looks like this:

$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'
    )
);

What I hope for is a solution like this:

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

What is the best way to achieve this? Use str_replace and replace the word in the file? I hope there's a better way of doing this, and I'd be very pleased if you could help me out.

EDIT: (my complete init.php file)

<?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;
}
  • 写回答

1条回答 默认 最新

  • doushao1087 2017-01-01 16:13
    关注

    This is somewhat opinion-based, but I use a json or xml file that the config file parses. The update would call the file, parse it then resave it.

    /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":"enabled","title_block":"enabled","menu":"enabled","slideshow":"enabled","left_box":"enabled","email_block":"enabled","footer":"enabled","keurmerken":"enabled","copyright":"enabled"}}

    Pseudo-code for your update:

    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
                }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料