2017-01-01 10 views
0

私は 'init.php'というファイルを持っています。これは、セッションを開始し、いくつかの設定とそのようなものを設定するために使用されています。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') 

これはもちろん非常に簡単です。しかし、今は自分でファイルを変更することなく設定を編集したいと思っています。それはすべてブラウザのスクリプトで行う必要があります。

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

これを達成するための最良の方法は何ですか?このような私のinit.php残りの設定は、グローバルなルックスstr_replaceを使用してファイル内の単語を置き換えますか?私はこれをやるより良い方法があることを願っています。あなたが私を助けてくれるなら、私はとても喜んでいます。

EDIT:(私の完全な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

これは、多少意見・ベースですが、私は、configファイルを解析し、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":"enabled","title_block":"enabled","menu":"enabled","slideshow":"enabled","left_box":"enabled","email_block":"enabled","footer":"enabled","keurmerken":"enabled","copyright":"enabled"}}

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 
      } 
    } 
+0

そして、あなたはなにを知っていますか? JSONを使用せずに彼女の方法? –

+0

XMLファイルですが、Jsonはより簡単な – Rasclatt

+0

ですが、getメソッドを使用しているときにこのメソッド(JSON)は動作しますか? –

関連する問題