我已经获得了文件' 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;
}
答案 0 :(得分:0)
这有些基于意见,但我使用配置文件解析的json或xml文件。 update
将调用该文件,解析它然后重新保存它。
<强> /core/init.php 强>
$GLOBALS['config'] = json_decode(__DIR__.DS.'prefs.json', true);
<强> /core/prefs.json 强>
{ “MySQL的”:{ “主机”: “本地主机:3307”, “用户名”: “根”, “密码”: “usbw”, “DB”: “网上商店”}, “记住”:{” cookie_name “:” 散”, “cookie_expiry”:604800}, “两会”:{ “会话名称”: “用户”, “TOKEN_NAME”: “令牌”}, “设置”:{ “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
}
}