Kohana编辑配置文件动态



我想知道我们是否可以动态编辑配置文件。

这是我的例子。

<?php
// config/system.php
return array(
    'data'=>"content",
    'data1' => "content2",
);
?>

我知道我们可以使用set()methode编辑它,但是此methode不编辑文件。

示例:

<?php
// get config file array
$config = Kohana::$config->load('system');
// set the new config .. but this function doesn't edit the file !
$config->set("data","MyContent");
?>

有什么想法吗?

最后我自己做了,也许这也可以帮助别人。

1-创建AppPath.'Config/group.php'并放置此脚本。

<?php defined('SYSPATH') OR die('No direct script access.');
// Save this file at APPPATH.'config/Group.php'
// Extend the original Config_group
class Config_Group extends Kohana_Config_Group {
    // This function allow us to save on the config file
    public function save()
    {
        $filename = APPPATH.'config'.DIRECTORY_SEPARATOR.$this->group_name().".php";
                // test if the config file is writable or not.
        if (is_writable($filename))
        {
                        // save the array into the config file and return true/false
            return (file_put_contents($filename, "<?php defined('SYSPATH') or die('No direct script access.');".PHP_EOL
                                    ."return " . var_export($this->as_array(), true) . ";",LOCK_EX));
        }
        return FALSE;
    }
}

如何使用:

<?php
// get config file array
$config = Kohana::$config->load('system');
// set the new config .. but this function doesn't edit the file !
$config->set("data","MyContent");
// Save the new file config.
$config->save();
?>

最新更新